iOS相机相册调用 — UIImagePickerController

来源:互联网 发布:陕西师大远程网络教育 编辑:程序博客网 时间:2024/06/05 11:58

在iOS开发中如果要调用相机拍取照片或者是直接获取相册中的照片,那么调用UIImagePickerController是个不错的选择。UIImagePickerController继承于UINavigationController,使用代理方法时需要同时遵守这两个控制器的协议,它不仅可以用来选取图片,其实它的还能用来拍摄视频。http://blog.csdn.net/magical_code/article/details/51132664

1.UIImagePickerController简介

UIImagePickerController是系统提供的用来获取图片或视频的接口,使用UIImagePickerController类来获取图片的基本步骤如下:

  • 初始化UIImagePickerController类
  • 设置UIImagePickerController实例的数据来源
  • 设置UIImagePickerController实例的代理
  • 设置是否允许编辑图片,若允许则allowsEditing属性值置为YES
  •  设置完UIImagePickerController实例的属性之后,在需要获取图片时要跳转到图像选取控制器当中去选取或拍摄图片
  • 完成图片的选取后回调代理方法

UIImagePickerController实例的三种数据来源:

  1. typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
  2. UIImagePickerControllerSourceTypePhotoLibrary, // 来自图库
  3. UIImagePickerControllerSourceTypeCamera, // 来自相机
  4. UIImagePickerControllerSourceTypeSavedPhotosAlbum // 来自相册
  5. };

2.实际测试相机和图库

本次构建的应用允许用户通过相机或者图库来选取一张图像并在imageView当中显示。

2-1.设计界面

在故事板中为程序的主界面添加一个imageView和一个button,并为他们添加两个输出接口:

QQ20160330-0

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
  4.  
  5. @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  6. - (IBAction)chooseImage:(id)sender;
  7.  
  8. @end

2-2.功能实现

在ViewController.m中实现按钮的点击方法:

  1. - (IBAction)chooseImage:(id)sender {
  2. // 创建UIImagePickerController实例
  3. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  4. // 设置代理
  5. imagePickerController.delegate = self;
  6. // 是否允许编辑(默认为NO)
  7. imagePickerController.allowsEditing = YES;
  8. // 创建一个警告控制器
  9. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选取图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  10. // 设置警告响应事件
  11. UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  12. // 设置照片来源为相机
  13. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  14. // 设置进入相机时使用前置或后置摄像头
  15. imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
  16. // 展示选取照片控制器
  17. [self presentViewController:imagePickerController animated:YES completion:^{}];
  18. }];
  19. UIAlertAction *photosAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  20. imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  21. [self presentViewController:imagePickerController animated:YES completion:^{}];
  22. }];
  23. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  24. }];
  25. // 判断是否支持相机
  26. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  27. {
  28. // 添加警告按钮
  29. [alert addAction:cameraAction];
  30. }
  31. [alert addAction:photosAction];
  32. [alert addAction:cancelAction];
  33. // 展示警告控制器
  34. [self presentViewController:alert animated:YES completion:nil];
  35. }

设置UIImagePickerController的代理方法:

  1. #pragma mark - UIImagePickerControllerDelegate
  2. // 完成图片的选取后调用的方法
  3. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  4. // 选取完图片后跳转回原控制器
  5. [picker dismissViewControllerAnimated:YES completion:nil];
  6. /* 此处参数 info 是一个字典,下面是字典中的键值 (从相机获取的图片和相册获取的图片时,两者的info值不尽相同)
  7. * UIImagePickerControllerMediaType; // 媒体类型
  8. * UIImagePickerControllerOriginalImage; // 原始图片
  9. * UIImagePickerControllerEditedImage; // 裁剪后图片
  10. * UIImagePickerControllerCropRect; // 图片裁剪区域(CGRect)
  11. * UIImagePickerControllerMediaURL; // 媒体的URL
  12. * UIImagePickerControllerReferenceURL // 原件的URL
  13. * UIImagePickerControllerMediaMetadata // 当数据来源是相机时,此值才有效
  14. */
  15. // 从info中将图片取出,并加载到imageView当中
  16. UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
  17. self.imageView.image = image;
  18. // 创建保存图像时需要传入的选择器对象(回调方法格式固定)
  19. SEL selectorToCall = @selector(image:didFinishSavingWithError:contextInfo:);
  20. // 将图像保存到相册(第三个参数需要传入上面格式的选择器对象)
  21. UIImageWriteToSavedPhotosAlbum(image, self, selectorToCall, NULL);
  22. }
  23.  
  24. // 取消选取调用的方法
  25. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  26. [self dismissViewControllerAnimated:YES completion:nil];
  27. }
  28.  

添加保存图片完成后的回调方法:

  1. // 保存图片后到相册后,回调的相关方法,查看是否保存成功
  2. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
  3. if (error == nil){
  4. NSLog(@"Image was saved successfully.");
  5. } else {
  6. NSLog(@"An error happened while saving the image.");
  7. NSLog(@"Error = %@", error);
  8. }
  9. }

 2-3.效果展示

IMG_0125

 

点击选择图片按钮:

IMG_0126

 

使用相机拍摄:

IMG_0127IMG_0128

IMG_0130

 

从图库中选取:

IMG_0134IMG_0131

 

点击choose选择该图片:

IMG_0133

0 0
原创粉丝点击