在iOS开发中,如果要调用相机拍照或直接获取相册中的照片,一般可以调用UIImagePickerController
来实现。
简介
UIImagePickerController
是系统提供的用来获取图片或视频的接口。
判断设备是否有摄像头
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[SVProgressHUD showNOmessage:@"您的设备没有摄像头"];
return ;
}
使用步骤
1、初始化UIImagePickerController
类
// 创建实例
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
2、设置UIImagePickerController
实例的数据来源
// --相机
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// 设置(默认)前置摄像头
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
//设置 照相机或摄像机
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
或
// --相册
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
3、设置UIImagePickerController
实例的代理
遵守两个协议,并设置代理
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
...
imagePicker.delegate = self;
4、设置是否允许编辑图片,默认为NO
imagePicker.allowsEditing = YES;
5、 设置完UIImagePickerController
实例的属性之后,在需要获取图片时要跳转到图像选取控制器当中去选取或拍摄图片
// 展示选取照片控制器
[self presentViewController:imagePicker animated:YES completion:^{}];
6、完成图片的选取后回调代理方法
#pragma mark - 代理方法 当得到照片或视频后,调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// 选取完图片后,跳转回原控制器
[picker dismissViewControllerAnimated:YES completion:nil];
// 从info中将图片取出,
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; //原图
//图片后续操作。。。
}
此处参数 info 是一个字典,其中的键值如下 (从相机获取或相册获取的图片时,两者的info值不尽相同)
* UIImagePickerControllerMediaType; // 媒体类型
* UIImagePickerControllerOriginalImage; // 原始图片
* UIImagePickerControllerEditedImage; // 编辑后图片
* UIImagePickerControllerCropRect; // 图片裁剪区域(CGRect)
* UIImagePickerControllerMediaURL; // 媒体的URL
* UIImagePickerControllerReferenceURL // 原件的URL
* UIImagePickerControllerMediaMetadata // 当数据来源是相机时,此值才有效
#pragma mark - 代理方法 保存图片到相册后,调用
- (void) imageWasSavedSuccessfully:(UIImage *)paramImage didFinishSavingWithError:(NSError *)paramError contextInfo:(void *)paramContextInfo{
if (paramError == nil){
NSLog(@"保存成功.");
} else {
NSLog(@"保存失败");
NSLog(@"Error = %@", paramError);
}
}
#pragma mark - 代理方法 取消选取调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
添加录像功能
- (void)ClickControlAction:(id)sender{
// 判断有摄像头,并且支持拍照功能
if ([self isCameraAvailable] && [self doesCameraSupportTakingPhotos]){
// 初始化
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
// 设置媒体来源(相机)
[controller setSourceType:UIImagePickerControllerSourceTypeCamera];
// 设置所支持的媒体功能,(即只能拍照,或只能录像,或两者都可)
NSString *requiredMediaType = ( NSString *)kUTTypeImage;
NSString *requiredMediaType1 = ( NSString *)kUTTypeMovie;
NSArray *arrMediaTypes=[NSArray arrayWithObjects:requiredMediaType, requiredMediaType1,nil];
[controller setMediaTypes:arrMediaTypes];
// 设置录制视频的质量
[controller setVideoQuality:UIImagePickerControllerQualityTypeHigh];
//设置最长摄像时间
[controller setVideoMaximumDuration:10.f];
// 设置是否可以管理已经存在的图片 或 视频
[controller setAllowsEditing:YES];
// 设置代理
[controller setDelegate:self];
[self presentViewController:controller animated:YES completion:nil];
}
}
从相册获取图片或视频数据
- (void)ClickShowPhotoAction:(id)sender{
if ([self isPhotoLibraryAvailable]){
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
// 设置类型
[controller setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
if ([self canUserPickPhotosFromPhotoLibrary]){
[mediaTypes addObject:( NSString *)kUTTypeImage];
}
if ([self canUserPickVideosFromPhotoLibrary]){
[mediaTypes addObject:( NSString *)kUTTypeMovie];
}
[controller setMediaTypes:mediaTypes];
[controller setDelegate:self]; // 设置代理
[self.navigationController presentViewController:controller animated:YES completion:nil];
}
}
自定义相机
https://www.brightec.co.uk/ideas/custom-ios-camera-replicates-system-camera-app
https://github.com/brightec/CustomCamera_Objc
参考资料
http://www.jianshu.com/p/097ed620cc38
https://www.cnblogs.com/xiangjune/p/5916599.html
http://blog.csdn.net/magical_code/article/details/51132664
http://www.jianshu.com/p/8c66aa445ec8
https://www.jianshu.com/p/48ffc684c881
https://blog.csdn.net/jueyi1127/article/details/46831875
代码
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
// 弹框选择框
... ...
// 选择照片
-(void)beginChoosePhoto{
// 创建实例
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
// 设置照片来源为相机
//imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// 设置(默认)前置摄像头
//imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
// 设置照片来源为相册
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = YES;
// 展示选取照片控制器
[self presentViewController:imagePicker animated:YES completion:^{
}];
}
// 拍照
-(void)beginTakePhoto{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[SVProgressHUD showNOmessage:@"未检测到摄像头"];
return ;
}
// 创建实例
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
// 设置照片来源为相机
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// 设置(默认)前置摄像头
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
// 展示选取照片控制器
[self presentViewController:imagePicker animated:YES completion:nil];
}
// 代理方法
#pragma mark - 代理方法 当得到照片或视频后,调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// 选取完图片后,跳转回原控制器
[picker dismissViewControllerAnimated:YES completion:nil];
// ke yi 裁剪 UIImagePickerControllerEditedImage(只能在相册选择中)
//UIImagePickerControllerOriginalImage
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if(!image){
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
image = [image rescaleImageToPX:300];
//提交后台
[self uploadHeader:image];
}
//取消选取调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}