公司項(xiàng)目需要,了解了一下UIImagePickerController的使用缠导,可以調(diào)用系統(tǒng)相機(jī)來進(jìn)行拍照和錄像等功能寥掐,通過在網(wǎng)上查閱資料有了解到一些知識(shí),下面是關(guān)于UIImagePickerController的屬性帕胆。
// 指定使用照相機(jī)模式,可以指定使用相冊(cè)/照片庫
imagepicker.sourceType = UIImagePickerControllerTypeCamera;
//設(shè)置當(dāng)拍照完或在相冊(cè)選完照片后般渡,是否跳到編輯模式進(jìn)行圖片剪裁惶楼。只有當(dāng)showsCameraControls屬性為true時(shí)才有效果
imagepicker.allowsEditing = YES;
//設(shè)置拍照時(shí)的下方的工具欄是否顯示,如果需要自定義拍攝界面诊杆,則可把該工具欄隱藏。
imagepicker.showsCameraControls = YES;
//設(shè)置使用后置攝像頭何陆,可以使用前置攝像頭
imagepicker.cameraDevice = UIImagePickerContorllerCameraDeviceRear;
//設(shè)置閃光燈模式
/* typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) { UIImagePickerControllerCameraFlashModeOff = -1, UIImagePickerControllerCameraFlashModeAuto = 0, UIImagePickerControllerCameraFlashModeOn = 1 }; imagepicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto; */
//設(shè)置相機(jī)支持的類型晨汹,拍照和錄像
imagepicker.mediaTypes = @[(NSString *)KUTTypeImage, (NSString *)KUTTypeMovie];
//設(shè)置拍攝時(shí)屏幕的view的transform屬性,可以實(shí)現(xiàn)旋轉(zhuǎn)贷盲,縮放功能
imagepicker.cameraViewTransform = CGAffineTransformMakeRotation(M_PI * 45/ 180); imagepicker.cameraViewTransform = CGAffineTransformMakeScale(1.5, 1.5);
//所有含有cameraXXX的屬性都必須要sourceType是UIImagePickerControllerSourceTypeCamera時(shí)設(shè)置才有效果淘这,否則會(huì)有異常
//設(shè)置UIImagePickerController的代理
imagepicker.delegate = self;
- (void)showCamera { [self presentViewController:imagepicker animated:YES completion:nil]; }
成功獲取相片還是視頻后的回調(diào)方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //通過UIImagePickerControllerMediaType判斷返回的是照片還是視頻 NSString *type = info[UIImagePickerControllerMediaType]; //如果返回的type等于kUTTypeImage,代表返回的是照片,并且需要判斷當(dāng)前相機(jī)使用的sourceType是拍照還是相冊(cè) if ([type isEqualToString: (NSString *)kUTTypeImage] && picker.sourceType == UIImagePickerControllerSourceTypeCamera) { //獲取照片的原圖 UIImage *originalImage = info[UIImagePickerControllerOriginalImage]; //獲取圖片裁剪的圖 UIImage *editImage = info[UIImagePickerControllerEditedImage]; //獲取圖片裁剪后巩剖,剩下的圖 UIImage *cropImage = info[UIImagePickerControllerCropRect]; //獲取圖片的url NSURL *url = info[UIImagePickerControllerMediaURL]; //獲取圖片的metadata數(shù)據(jù)信息 NSDictionary *metadata = info[UIImagePickerControllerMediaMetadata]; //如果是拍照的照片铝穷,則需要手動(dòng)保存到本地,系統(tǒng)不會(huì)自動(dòng)保存拍照成功后的照片 UIImageWriteToSavedPhotosAlbum(editImage, self, @selector(image: didFinishSavingWithError: contextInfo:), nil); } else { } //退出UIImagePickerController [imagepicker dismissViewControllerAnimated:YES completion:nil]; }
//取消照相機(jī) 的回調(diào)
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [imagepicker dismissViewControllerAnimated:YES completion:nil]; }
//保存照片成功后的回調(diào)
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { NSLog(@"保存成功..."); } 如果想要自定義拍攝界面佳魔,需要設(shè)置拍照時(shí)的下方工具欄顯示為NO
imagepicker.showsCameraControls = NO;`
然后在imagepicker的cameraOverlayView上設(shè)置自己自定義視圖界面就行李曙聂。