公司项目需要,了解了一下UIImagePickerController的使用,可以调用系统相机来进行拍照和录像等功能,通过在网上查阅资料有了解到一些知识,下面是关于UIImagePickerController的属性。
// 指定使用照相机模式,可以指定使用相册/照片库
imagepicker.sourceType = UIImagePickerControllerTypeCamera;
//设置当拍照完或在相册选完照片后,是否跳到编辑模式进行图片剪裁。只有当showsCameraControls属性为true时才有效果
imagepicker.allowsEditing = YES;
//设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏。
imagepicker.showsCameraControls = YES;
//设置使用后置摄像头,可以使用前置摄像头
imagepicker.cameraDevice = UIImagePickerContorllerCameraDeviceRear;
//设置闪光灯模式
/* typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) { UIImagePickerControllerCameraFlashModeOff = -1, UIImagePickerControllerCameraFlashModeAuto = 0, UIImagePickerControllerCameraFlashModeOn = 1 }; imagepicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto; */
//设置相机支持的类型,拍照和录像
imagepicker.mediaTypes = @[(NSString *)KUTTypeImage, (NSString *)KUTTypeMovie];
//设置拍摄时屏幕的view的transform属性,可以实现旋转,缩放功能
imagepicker.cameraViewTransform = CGAffineTransformMakeRotation(M_PI * 45/ 180); imagepicker.cameraViewTransform = CGAffineTransformMakeScale(1.5, 1.5);
//所有含有cameraXXX的属性都必须要sourceType是UIImagePickerControllerSourceTypeCamera时设置才有效果,否则会有异常
//设置UIImagePickerController的代理
imagepicker.delegate = self;
- (void)showCamera { [self presentViewController:imagepicker animated:YES completion:nil]; }
成功获取相片还是视频后的回调方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //通过UIImagePickerControllerMediaType判断返回的是照片还是视频 NSString *type = info[UIImagePickerControllerMediaType]; //如果返回的type等于kUTTypeImage,代表返回的是照片,并且需要判断当前相机使用的sourceType是拍照还是相册 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数据信息 NSDictionary *metadata = info[UIImagePickerControllerMediaMetadata]; //如果是拍照的照片,则需要手动保存到本地,系统不会自动保存拍照成功后的照片 UIImageWriteToSavedPhotosAlbum(editImage, self, @selector(image: didFinishSavingWithError: contextInfo:), nil); } else { } //退出UIImagePickerController [imagepicker dismissViewControllerAnimated:YES completion:nil]; }
//取消照相机 的回调
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [imagepicker dismissViewControllerAnimated:YES completion:nil]; }
//保存照片成功后的回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { NSLog(@"保存成功..."); } 如果想要自定义拍摄界面,需要设置拍照时的下方工具栏显示为NO
imagepicker.showsCameraControls = NO;`
然后在imagepicker的cameraOverlayView上设置自己自定义视图界面就行李。