UIImagePickerController的详细使用#
一、UIImagePickerController的功能##
- 从相机、相册、图库获取媒体
- 从相机、相册、图库获取媒体
- 定制相机界面
二、UIImagePickerController的基本使用##
1. 从相册获取媒体
从当前页面进入媒体选择页面
// 判断当前的sourceType是否可用
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
// 实例化UIImagePickerController控制器
UIImagePickerController * imagePickerVC = [[UIImagePickerController alloc] init];
// 设置资源来源(相册、相机、图库之一)
imagePickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 设置可用的媒体类型、默认只包含kUTTypeImage,如果想选择视频,请添加kUTTypeMovie
// 如果选择的是视屏,允许的视屏时长为20秒
imagePickerVC.videoMaximumDuration = 20;
// 允许的视屏质量(如果质量选取的质量过高,会自动降低质量)
imagePickerVC.videoQuality = UIImagePickerControllerQualityTypeHigh;
imagePickerVC.mediaTypes = @[(NSString *)kUTTypeMovie, (NSString *)kUTTypeImage];
// 设置代理,遵守UINavigationControllerDelegate, UIImagePickerControllerDelegate 协议
imagePickerVC.delegate = self;
// 是否允许编辑(YES:图片选择完成进入编辑模式)
imagePickerVC.allowsEditing = YES;
// model出控制器
[self presentViewController:imagePickerVC animated:YES completion:nil];
选择完成、取消选择调用代理方法:故实现UIImagePickerController的代理方法
// 选择图片成功调用此方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// dismiss UIImagePickerController
[self dismissViewControllerAnimated:YES completion:nil];
// 选择的图片信息存储于info字典中
NSLog(@"%@", info);
}
// 取消图片选择调用此方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
// dismiss UIImagePickerController
[self dismissViewControllerAnimated:YES completion:nil];
}
注:info中可能包含的key的含义
UIImagePickerControllerCropRect // 编辑裁剪区域
UIImagePickerControllerEditedImage // 编辑后的UIImage
UIImagePickerControllerMediaType // 返回媒体的媒体类型
UIImagePickerControllerOriginalImage // 原始的UIImage
UIImagePickerControllerReferenceURL // 图片地址
从图库获取与从相册获取一样,只不过 sourceType 换成 UIImagePickerControllerSourceTypeSavedPhotosAlbum
2.从相机中获取
if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
// 实例化UIImagePickerController控制器
UIImagePickerController * imagePickerVC = [[UIImagePickerController alloc] init];
// 设置资源来源
imagePickerVC.sourceType = sourceType;
// 设置可用的媒体类型、默认只包含kUTTypeImage,如果想选择视频,请添加kUTTypeMovie
imagePickerVC.mediaTypes = @[(NSString *)kUTTypeMovie, (NSString *)kUTTypeImage];
// 设置代理,遵守UINavigationControllerDelegate, UIImagePickerControllerDelegate 协议
imagePickerVC.delegate = self;
// 是否允许编辑
imagePickerVC.allowsEditing = YES;
// 如果选择的是视屏,允许的视屏时长为20秒
imagePickerVC.videoMaximumDuration = 20;
// 允许的视屏质量(如果质量选取的质量过高,会自动降低质量)
imagePickerVC.videoQuality = UIImagePickerControllerQualityTypeHigh;
// 相机获取媒体的类型(照相、录制视屏)
imagePickerVC.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
// 使用前置还是后置摄像头
imagePickerVC.cameraDevice = UIImagePickerControllerCameraDeviceRear;
// 是否看起闪光灯
imagePickerVC.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
imagePickerVC.showsCameraControls = NO;
// model出控制器
[self presentViewController:imagePickerVC animated:YES completion:nil];
}
选择完成、取消选择调用代理方法:故实现UIImagePickerController的代理方法
// 选择图片成功调用此方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// dismiss UIImagePickerController
[self dismissViewControllerAnimated:YES completion:nil];
// 选择的图片信息存储于info字典中
NSLog(@"%@", info);
}
// 取消图片选择调用此方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
// dismiss UIImagePickerController
[self dismissViewControllerAnimated:YES completion:nil];
}
3.定制相机界面
定制相机界面,只需要给UIImagePickerController添加如下的一行代码,同时提供自己的界面即可(定制界面即定制拍照按钮等)
1.设置自定义相机界面
// 设置是否显示系统的相机页面(我们要定制则返回NO)
imagePickerVC.showsCameraControls = NO;
2.提供自己的拍照界面
// 拍照界面容器
UIView * customCameraView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 开始摄像按钮(如果是拍照,则不需要此按钮)
UIButton * start = [UIButton buttonWithType:UIButtonTypeCustom];
start.frame = CGRectMake(0, 0, 44, 100);
[start setTitle:@"开始" forState:UIControlStateNormal];
[start addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchDown];
[customCameraView addSubview:start];
// 停止摄像按钮(如果是拍照,则不需要此按钮)
UIButton * stop = [UIButton buttonWithType:UIButtonTypeCustom];
stop.frame = CGRectMake(200, 0, 44, 100);
[stop setTitle:@"停止" forState:UIControlStateNormal];
[stop addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchDown];
[customCameraView addSubview:stop];
// 拍照按钮(如果是摄像,则不需要此按钮)
UIButton * takePicture = [UIButton buttonWithType:UIButtonTypeCustom];
takePicture.frame = CGRectMake(200, 100, 44, 100);
[takePicture setTitle:@"照相" forState:UIControlStateNormal];
[takePicture addTarget:self action:@selector(takePicture) forControlEvents:UIControlEventTouchDown];
[customCameraView addSubview:takePicture];
// 将自定义的相机界面赋值给cameraOverlayView属性即可显示自定义界面
imagePickerVC.cameraOverlayView = customCameraView;
// 此属性可transform自定义界面
imagePickerVC.cameraViewTransform = CGAffineTransformMakeScale(0.5, 0.5);
3.实现按钮点击事件
- (void)start {
// 开始摄像
[self.imagePickerVC startVideoCapture];
}
- (void)stop {
// 停止摄像
[self.imagePickerVC stopVideoCapture];
}
- (void)takePicture {
// 拍照
[self.imagePickerVC takePicture];
}
如果想实现更优雅的界面,请自行布局,本例只是简单的说明怎么使用
注意:
1> 调用了 [self.imagePickerVC takePicture]; 方法后会自动调用代理的 imagePickerController:didFinishPickingImage:editingInfo: 方法,在此方法中可不dismiss imagePic可Controller,则可以实现多联拍
三、UIImagePickerController补充
因为 UIImagePickerController 是继承至 UINavigationController,所以可以push和pop一些viewcontroller进行导航效果。例如,自定义照相机画面的时候可以在拍摄完后push一个viewcontroller用于对照片进行编辑。
本文只是自己的一些探索,如有错误,请message
另外,系统提供的UIImagePickerController只能实现单选,要实现多选,请关注我的下一篇文章:自定义UIImagePickerController