在实际的APP开发中,我们经常会见到应用的这样的功能 :需要选取手机相册的照片,还有选取视频,拍视频和照相的操作.
在iOS开发中,实现以上的功能就需要用到 UIImagePickerController.
现将 UIImagePickerController的基本用法总结如下 :
1 #import "ViewController.h" 2 3 @interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate> 4 5 //拍照 6 - (IBAction)takePhoto:(id)sender; 7 8 //拍电影 9 - (IBAction)takeMovie:(id)sender; 10 11 @property (weak, nonatomic) IBOutlet UIImageView *imgView; 12 13 //选择照片 14 - (IBAction)selectPhoto:(id)sender; 15 16 //选择视频 17 - (IBAction)selectVideo:(id)sender; 18 19 20 @end 21 22 @implementation ViewController 23 24 - (void)viewDidLoad { 25 [super viewDidLoad]; 26 27 //UIImagePickerController.访问用户相册 2.拍照,拍视频 28 29 } 30 31 //1.选取照片 32 - (IBAction)selectPhoto:(id)sender { 33 34 UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init]; 35 36 /* 37 UIImagePickerControllerSourceTypePhotoLibrary,获取相册中所有的文件 38 UIImagePickerControllerSourceTypeCamera,摄像头 39 UIImagePickerControllerSourceTypeSavedPhotosAlbum,系统内置相册 40 */ 41 42 //资源类型(资源来自哪里,可以来相册,摄像头) 43 imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 44 45 //设置代理 46 imagePickerCtrl.delegate = self; 47 48 //弹出控制器 49 [self presentViewController:imagePickerCtrl animated:YES completion:^{ 50 51 //弹出控制器完成调用的方法 52 }]; 53 54 } 55 /** 56 * 57 * 2.选取视频 58 */ 59 - (IBAction)selectVideo:(id)sender { 60 61 UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init]; 62 63 imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 64 imagePickerCtrl.delegate = self; 65 66 //指定媒体类型,图片(@"public.image"),视频(@"public.movie") 67 imagePickerCtrl.mediaTypes = @[/*@"public.image",*/@"public.movie"]; 68 69 [self presentViewController:imagePickerCtrl animated:YES completion:NULL]; 70 71 72 } 73 74 //3.拍照 75 - (IBAction)takePhoto:(id)sender { 76 77 UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init]; 78 79 imagePickerCtrl.delegate = self; 80 81 //判断手机是否支持(前)摄像头 82 if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) { 83 84 UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机太渣,没有摄像头" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; 85 [alerView show]; 86 87 return; 88 } 89 90 //指定资源来自摄像头 91 imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeCamera; 92 93 [self presentViewController:imagePickerCtrl animated:YES completion:NULL]; 94 95 96 } 97 98 //4.拍视频 99 - (IBAction)takeMovie:(id)sender { 100 101 UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init]; 102 103 imagePickerCtrl.delegate = self; 104 105 //指定资源来自摄像头 106 imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeCamera; 107 108 //设置资源类型 109 imagePickerCtrl.mediaTypes = @[@"public.movie"]; 110 111 [self presentViewController:imagePickerCtrl animated:YES completion:NULL]; 112 } 113 114 #pragma mark UIImagePickerControllerDelegate 115 //选取完视频,或者照片调用的协议方法(不管是拍照,选相册,拍视频,选视频,最终都会调用此方法) 116 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 117 118 NSLog(@"%@",info); 119 120 //取出选择的照片 121 UIImage *img = info[UIImagePickerControllerOriginalImage]; 122 123 //将选取的照片交给_imgView显示 124 _imgView.image = img; 125 126 127 //如果资源来自摄像头 128 if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { 129 130 //将图片存到相册中调用的方法 (苹果建议的写法:好像必须要这样写) 131 //- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo 132 133 UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 134 } 135 136 //如果选中的是视频则可以通过UIImagePickerControllerMediaURL在字典中获取到选中的视频的URL 137 138 //选取照片或视频后,关闭控制器 139 [picker dismissViewControllerAnimated:YES completion:NULL]; 140 141 } 142 143 //"取消"按钮被点击 144 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 145 146 NSLog(@"被关闭了"); 147 [picker dismissViewControllerAnimated:YES completion:NULL]; 148 149 } 150 151 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ 152 153 NSLog(@"照片保存到相册成功"); 154 } 155 156 @end
这种写法的缺点就是,选取图片的时候只能选取一张照片,如果要选取多张照片的话,就需要用到另外一种方法 AssetsLibrary
AssetsLibrary的基本用法随后更新!!! 记得关注我哟!!!!