在iOS开发中如果要调用相机拍取照片或者是直接获取相册中的照片,那么调用UIImagePickerController是个不错的选择。UIImagePickerController继承于UINavigationController,使用代理方法时需要同时遵守这两个控制器的协议,它不仅可以用来选取图片,其实它的还能用来拍摄视频。
1.UIImagePickerController简介
UIImagePickerController是系统提供的用来获取图片或视频的接口,使用UIImagePickerController类来获取图片的基本步骤如下:
- 初始化UIImagePickerController类
- 设置UIImagePickerController实例的数据来源
- 设置UIImagePickerController实例的代理
- 设置是否允许编辑图片,若允许则allowsEditing属性值置为YES
- 设置完UIImagePickerController实例的属性之后,在需要获取图片时要跳转到图像选取控制器当中去选取或拍摄图片
- 完成图片的选取后回调代理方法
UIImagePickerController实例的三种数据来源:
-
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
-
UIImagePickerControllerSourceTypePhotoLibrary, // 来自图库
-
UIImagePickerControllerSourceTypeCamera, // 来自相机
-
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 来自相册
- };
2.实际测试相机和图库
本次构建的应用允许用户通过相机或者图库来选取一张图像并在imageView当中显示。
2-1.设计界面
在故事板中为程序的主界面添加一个imageView和一个button,并为他们添加两个输出接口:
![QQ20160330-0](http://7xow65.com1.z0.glb.clouddn.com/wp-content/uploads/2016/03/QQ20160330-01.png)
- #import <UIKit/UIKit.h>
-
-
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
-
-
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
-
- (IBAction)chooseImage:(id)sender;
-
- @end
2-2.功能实现
在ViewController.m中实现按钮的点击方法:
-
- (IBAction)chooseImage:(id)sender {
-
// 创建UIImagePickerController实例
-
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
-
// 设置代理
-
imagePickerController.delegate = self;
-
// 是否允许编辑(默认为NO)
-
imagePickerController.allowsEditing = YES;
-
-
-
// 创建一个警告控制器
-
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选取图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
-
// 设置警告响应事件
-
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
// 设置照片来源为相机
-
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
-
-
// 设置进入相机时使用前置或后置摄像头
-
imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
-
-
// 展示选取照片控制器
-
[self presentViewController:imagePickerController animated:YES completion:^{}];
-
}];
-
-
UIAlertAction *photosAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
-
[self presentViewController:imagePickerController animated:YES completion:^{}];
-
}];
-
-
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
-
}];
-
-
// 判断是否支持相机
-
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
-
{
-
// 添加警告按钮
-
[alert addAction:cameraAction];
-
}
-
[alert addAction:photosAction];
-
[alert addAction:cancelAction];
-
// 展示警告控制器
-
[self presentViewController:alert animated:YES completion:nil];
- }
设置UIImagePickerController的代理方法:
-
#pragma mark - UIImagePickerControllerDelegate
- // 完成图片的选取后调用的方法
-
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
-
-
// 选取完图片后跳转回原控制器
-
[picker dismissViewControllerAnimated:YES completion:nil];
-
-
/* 此处参数 info 是一个字典,下面是字典中的键值 (从相机获取的图片和相册获取的图片时,两者的info值不尽相同)
- * UIImagePickerControllerMediaType; // 媒体类型
- * UIImagePickerControllerOriginalImage; // 原始图片
- * UIImagePickerControllerEditedImage; // 裁剪后图片
- * UIImagePickerControllerCropRect; // 图片裁剪区域(CGRect)
- * UIImagePickerControllerMediaURL; // 媒体的URL
- * UIImagePickerControllerReferenceURL // 原件的URL
- * UIImagePickerControllerMediaMetadata // 当数据来源是相机时,此值才有效
- */
-
-
// 从info中将图片取出,并加载到imageView当中
-
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
-
self.imageView.image = image;
-
-
// 创建保存图像时需要传入的选择器对象(回调方法格式固定)
-
SEL selectorToCall = @selector(image:didFinishSavingWithError:contextInfo:);
-
// 将图像保存到相册(第三个参数需要传入上面格式的选择器对象)
-
UIImageWriteToSavedPhotosAlbum(image, self, selectorToCall, NULL);
- }
-
- // 取消选取调用的方法
-
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
-
[self dismissViewControllerAnimated:YES completion:nil];
- }
-
添加保存图片完成后的回调方法:
- // 保存图片后到相册后,回调的相关方法,查看是否保存成功
-
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
-
if (error == nil){
-
NSLog(@"Image was saved successfully.");
-
} else {
-
NSLog(@"An error happened while saving the image.");
-
NSLog(@"Error = %@", error);
-
}
- }
2-3.效果展示
![IMG_0125](http://7xow65.com1.z0.glb.clouddn.com/wp-content/uploads/2016/03/IMG_0125-577x1024.jpg)
点击选择图片按钮:
![IMG_0126](http://7xow65.com1.z0.glb.clouddn.com/wp-content/uploads/2016/03/IMG_0126-577x1024.jpg)
使用相机拍摄:
![IMG_0127](http://7xow65.com1.z0.glb.clouddn.com/wp-content/uploads/2016/03/IMG_0127-577x1024.jpg)
![IMG_0128](http://7xow65.com1.z0.glb.clouddn.com/wp-content/uploads/2016/03/IMG_0128-577x1024.jpg)
![IMG_0130](http://7xow65.com1.z0.glb.clouddn.com/wp-content/uploads/2016/03/IMG_0130-577x1024.jpg)
从图库中选取:
![IMG_0134](http://7xow65.com1.z0.glb.clouddn.com/wp-content/uploads/2016/03/IMG_0134-577x1024.jpg)
![IMG_0131](http://7xow65.com1.z0.glb.clouddn.com/wp-content/uploads/2016/03/IMG_0131-577x1024.jpg)
点击choose选择该图片:
![IMG_0133](http://7xow65.com1.z0.glb.clouddn.com/wp-content/uploads/2016/03/IMG_0133-577x1024.jpg)