開啟相機:
//先設定sourceType為相機,然後判斷相機是否可用(ipod)沒相機,不可用將sourceType設定為相片庫 UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;// if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {// sourceType = UIImagePickerControllerSourceTypePhotoLibrary;// } //sourceType = UIImagePickerControllerSourceTypeCamera; //照相機 //sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //圖片庫 //sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //儲存的相片 UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化 picker.delegate = self; picker.allowsEditing = YES;//設定可編輯 picker.sourceType = sourceType; [self presentModalViewController:picker animated:YES];//進入照相介面 [picker release];
開啟相簿:(區分pad和iphone)
for iphone:
UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init]; if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //pickerImage.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType]; } pickerImage.delegate = self; pickerImage.allowsEditing = NO; [self presentModalViewController:pickerImage animated:YES]; [pickerImage release];
for ipad:
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //儲存的相片 UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = NO;//是否允許編輯 picker.sourceType = sourceType; /* 如果從一個導覽按鈕處呈現,使用: presentPopoverFromBarButtonItem:permittedArrowDirections:animated:; 如果要從一個視圖出呈現,使用: presentPopoverFromRect:inView:permittedArrowDirections:animated: 如果裝置旋轉以後,位置定位錯誤需要在父視圖控制器的下面方法裡面重新置放: didRotateFromInterfaceOrientation:(在這個方法體裡面重新設定rect) 然後再次調用: - (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated */ //UIPopoverController只能在ipad裝置上面使用;作用是用於顯示臨時內容,特點是總是顯示在當前視圖最前端,當單擊介面的其他地方時自動消失。 UIPopoverController *popover = [[UIPopoverController alloc]initWithContentViewController:picker]; self.imagePicker = popover; //permittedArrowDirections 設定箭頭方向 [self.imagePicker presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; [picker release]; [popover release];
點擊相簿中的圖片 貨照相機照完後點擊use 後觸發的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
*)info
點擊cancel 調用的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
例子:http://download.csdn.net/download/like7xiaoben/4958356