弹出照片选择器

 1 #import "ViewController.h"
 2 
 3 @interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
 4 
 5 /**
 6  *  选择图片
 7  */
 8 - (IBAction)selectedPhoto;
 9 
10 /**
11  *  显示被选中的图片
12  */
13 @property (weak, nonatomic) IBOutlet UIImageView *customIV;
14 
15 @end
16 
17 @implementation ViewController
18 
19 - (IBAction)selectedPhoto
20 {
21     // 1.创建图片选择控制器
22     UIImagePickerController *picker = [[UIImagePickerController alloc] init];
23     
24     // 2.设置代理
25     picker.delegate = self;
26     
27     // 3.判断图库是否能够打开
28     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
29         // 4.设置要打开图库的类型
30         picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
31     }
32     
33     // 5.弹出照片选择控制器
34     [self presentViewController:picker animated:YES completion:nil];
35 }
36 
37 #pragma mark - UIImagePickerControllerDelegate
38 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
39 {
40     /*
41     info = {
42         UIImagePickerControllerMediaType = "public.image";
43         UIImagePickerControllerOriginalImage = "<UIImage: 0x7f8912808270> size {1500, 1001} orientation 0 scale 1.000000";
44         UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=6E5438ED-9A8C-4ED0-9DEA-AB2D8F8A9360&ext=JPG";
45     }
46      */
47     // 保存被选中的图片
48     UIImage *selectedImg = info[UIImagePickerControllerOriginalImage];
49     self.customIV.image = selectedImg;
50     
51     // dismiss图片选择控制器
52     [picker dismissViewControllerAnimated:YES completion:nil];
53 }
54 
55 
56 @end
原文地址:https://www.cnblogs.com/Rinpe/p/4754124.html