iOS相机相册调用 — UIImagePickerController
2016-04-12 13:39
459 查看
在iOS开发中如果要调用相机拍取照片或者是直接获取相册中的照片,那么调用UIImagePickerController是个不错的选择。UIImagePickerController继承于UINavigationController,使用代理方法时需要同时遵守这两个控制器的协议,它不仅可以用来选取图片,其实它的还能用来拍摄视频。
UIImagePickerController是系统提供的用来获取图片或视频的接口,使用UIImagePickerController类来获取图片的基本步骤如下:
初始化UIImagePickerController类
设置UIImagePickerController实例的数据来源
设置UIImagePickerController实例的代理
设置是否允许编辑图片,若允许则allowsEditing属性值置为YES
设置完UIImagePickerController实例的属性之后,在需要获取图片时要跳转到图像选取控制器当中去选取或拍摄图片
完成图片的选取后回调代理方法
UIImagePickerController实例的三种数据来源:
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary, // 来自图库
UIImagePickerControllerSourceTypeCamera, // 来自相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 来自相册
};
本次构建的应用允许用户通过相机或者图库来选取一张图像并在imageView当中显示。
在故事板中为程序的主界面添加一个imageView和一个button,并为他们添加两个输出接口:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)chooseImage:(id)sender;
@end
在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);
}
}
点击选择图片按钮:
使用相机拍摄:
从图库中选取:
点击choose选择该图片:
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,并为他们添加两个输出接口:#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.效果展示
点击选择图片按钮:
使用相机拍摄:
从图库中选取:
点击choose选择该图片:
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- iOS推送的那些事
- IOS 改变键盘颜色代码