前言
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImagePickerController : UINavigationController
@available(iOS 2.0, *) public class UIImagePickerController : UINavigationController, NSCoding
iOS 获取图片有三种方法:
- 直接调用摄像头拍照;
- 从相册中选择;
- 从图库中选择。
UIImagePickerController 是系统提供的用来获取图片和视频的接口。用 UIImagePickerController 类来获取图片视频,大体分为以下几个步骤:
- 初始化 UIImagePickerController 类;
- 设置 UIImagePickerController 实例的数据来源类型;
- 设置代理;
- 如果需要做图片修改的话设置 allowsEditing = YES。
1、imagePickerController 的创建
Objective-C
需遵守协议 UIImagePickerControllerDelegate, UINavigationControllerDelegate
// 实例化 UIImagePickerController 对象 UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; // 设置代理 imagePickerController.delegate = self; // 设置是否需要做图片编辑,default NO imagePickerController.allowsEditing = YES; // 判断数据来源是否可用 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { // 设置数据来源 imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 打开相机/相册/图库 [self presentViewController:imagePickerController animated:YES completion:nil]; } // UIImagePickerControllerDelegate 协议方法 // 取消选择 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { // 退出当前界面 [picker dismissViewControllerAnimated:YES completion:nil]; } // 选择完成 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)]; [self.view addSubview:imageView]; // 获取点击的图片 imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage]; [picker dismissViewControllerAnimated:YES completion:nil]; }
Swift
需遵守协议 UIImagePickerControllerDelegate, UINavigationControllerDelegate
// 实例化 UIImagePickerController 对象 let imagePickerController = UIImagePickerController() // 设置代理 imagePickerController.delegate = self // 设置是否需要做图片编辑,default NO. imagePickerController.allowsEditing = true // 判断数据来源是否可用 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) { // 设置数据来源 imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary // 打开相机/相册/图库 self.presentViewController(imagePickerController, animated: true, completion: nil) } // UIImagePickerControllerDelegate 协议方法 // 取消选择 func imagePickerControllerDidCancel(picker: UIImagePickerController) { // 退出当前界面 picker.dismissViewControllerAnimated(true, completion: nil) } // 选择完成 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { let imageView = UIImageView(frame: CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)) self.view.addSubview(imageView) // 获取点击的图片 imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage picker.dismissViewControllerAnimated(true, completion: nil) }
2、imagePickerController 的设置
Objective-C
// 设置代理,需遵守 UIImagePickerControllerDelegate, UINavigationControllerDelegate 协议 imagePickerController.delegate = self; // 设置是否需要做图片编辑 imagePickerController.allowsEditing = YES; // 判断设备数据来源是否支持 /* UIImagePickerControllerSourceTypePhotoLibrary, // 来自图库 UIImagePickerControllerSourceTypeCamera, // 来自相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum // 来自相册 */ if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { } // 判断摄像头是否支持 /* UIImagePickerControllerCameraDeviceRear, // 后置摄像头 UIImagePickerControllerCameraDeviceFront // 前置摄像头 */ if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) { } // 判断闪光灯是否支持 if ([UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceFront]) { } // 获取数据来源支持的媒体类型 /* UIImagePickerControllerSourceTypePhotoLibrary, // 来自图库 "public.image", "public.movie" UIImagePickerControllerSourceTypeCamera, // 来自相机 ... UIImagePickerControllerSourceTypeSavedPhotosAlbum // 来自相册 "public.image", "public.movie" */ NSArray *mediaTypesArray = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum]; // 获取相机拥有的模式 /* UIImagePickerControllerCameraDeviceRear, // 后置摄像头 ... UIImagePickerControllerCameraDeviceFront // 前置摄像头 ... */ NSArray *captureModesArray = [UIImagePickerController availableCaptureModesForCameraDevice:UIImagePickerControllerCameraDeviceFront]; // 设置数据来源 /* UIImagePickerControllerSourceTypePhotoLibrary, // 来自图库,默认 UIImagePickerControllerSourceTypeCamera, // 来自相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum // 来自相册 */ imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 设置媒体类型 /* kUTTypeImage 包含: kUTTypeImage; // 抽象的图片类型 kUTTypeJPEG; kUTTypeJPEG2000; kUTTypeTIFF; kUTTypePICT; kUTTypeGIF; kUTTypePNG; kUTTypeQuickTimeImage; kUTTypeAppleICNS; kUTTypeBMP; kUTTypeICO kUTTypeMovie 包含: kUTTypeAudiovisualContent; // 抽象的声音视频 kUTTypeMovie; // 抽象的媒体格式(声音和视频) kUTTypeVideo; // 只有视频没有声音 kUTTypeAudio; // 只有声音没有视频 kUTTypeQuickTimeMovie; kUTTypeMPEG; kUTTypeMPEG4; kUTTypeMP3; kUTTypeMPEG4Audio; kUTTypeAppleProtectedMPEG4Audio 需要 #import
才能用 kUTTypeImage 和 KUTTypeMovie 。 default value is an array containing kUTTypeImage. */ imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie]; // 设置摄像头 /* UIImagePickerControllerCameraDeviceRear, // 后置摄像头,默认 UIImagePickerControllerCameraDeviceFront // 前置摄像头 */ imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront; // 设置相机模式 /* UIImagePickerControllerCameraCaptureModePhoto, // 照相模式,默认 UIImagePickerControllerCameraCaptureModeVideo // 录像模式 */ imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; // 设置闪光灯模式 /* UIImagePickerControllerCameraFlashModeOff = -1, // 关 UIImagePickerControllerCameraFlashModeAuto = 0, // 自动,默认 UIImagePickerControllerCameraFlashModeOn = 1 // 开 */ imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn; // 设置录像质量 /* UIImagePickerControllerQualityTypeHigh = 0, // 高质量,highest quality UIImagePickerControllerQualityTypeMedium = 1, // 中质量,默认,Wi-Fi UIImagePickerControllerQualityTypeLow = 2, // 低质量,cellular network UIImagePickerControllerQualityType640x480 = 3, // VGA 质量,VGA quality UIImagePickerControllerQualityTypeIFrame1280x720 = 4, UIImagePickerControllerQualityTypeIFrame960x540 = 5 apply only if mediaTypes includes kUTTypeMovie */ imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh; // 设置录像时间长度 /* default is 10 minutes. apply only mediaTypes includes kUTTypeMovie */ imagePickerController.videoMaximumDuration = 600; // 设置是否显示相机原生 UI /* set to NO to hide all standard camera UI. default is YES. available sourceType is Camera. */ imagePickerController.showsCameraControls = YES; // 自定义相机视图 /* set a view to overlay the preview view. default is YES. available sourceType is Camera. */ imagePickerController.cameraOverlayView = myView; // 改变相机视图 /* set the transform of the preview view. default is YES. available sourceType is Camera. 旋转 45 度,需要输入的参数为弧度,45/180 * M_PI,1 度 = PI/180 弧度 */ imagePickerController.cameraViewTransform = CGAffineTransformMakeRotation(0.25 * M_PI); // 打开相机/相册/图库 /* 访问设置的数据来源界面 */ [self presentViewController:imagePickerController animated:YES completion:nil]; // 拍照 /* 拍摄照片 */ [imagePickerController takePicture]; // 录像 // 开始录像 [imagePickerController startVideoCapture]; // 停止录像 [imagePickerController stopVideoCapture]; // 退出当前界面(相机/相册/图库) /* 在 UIImagePickerControllerDelegate 协议方法中调用 */ [picker dismissViewControllerAnimated:YES completion:nil]; // 获取选取媒体类型(图片或者视频) /* 选取的信息都在 info 中,info 是一个字典。字典中的键: UIImagePickerControllerMediaType; // 指定用户选择的媒体类型,包含着 kUTTypeImage 和 kUTTypeMovie UIImagePickerControllerOriginalImage; // 原始图片 UIImagePickerControllerEditedImage; // 修改后的图片,只有打开编辑模式 info 里才有此键值对 UIImagePickerControllerCropRect; // 裁剪尺寸,只有打开编辑模式 info 里才有此键值对 UIImagePickerControllerMediaURL; // 媒体的 URL UIImagePickerControllerReferenceURL; // 原件的 URL UIImagePickerControllerMediaMetadata // 当数据来源是照相机的时候这个值才有效 kUTTypeImage 包含: kUTTypeImage; // 抽象的图片类型 kUTTypeJPEG; kUTTypeJPEG2000; kUTTypeTIFF; kUTTypePICT; kUTTypeGIF; kUTTypePNG; kUTTypeQuickTimeImage; kUTTypeAppleICNS; kUTTypeBMP; kUTTypeICO kUTTypeMovie 包含: kUTTypeAudiovisualContent; // 抽象的声音视频 kUTTypeMovie; // 抽象的媒体格式(声音和视频) kUTTypeVideo; // 只有视频没有声音 kUTTypeAudio; // 只有声音没有视频 kUTTypeQuickTimeMovie; kUTTypeMPEG; kUTTypeMPEG4; kUTTypeMP3; kUTTypeMPEG4Audio; kUTTypeAppleProtectedMPEG4Audio 需要 #import 才能用 kUTTypeImage 和 KUTTypeMovie 。 */ // 直接处理点击的媒体资源 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)]; [self.view addSubview:imageView]; // 获取点击的图片 imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage]; // 先判断点击的资源类型再处理 // 判断点击的媒体资源类型 if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeImage]) { UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, 100, 100)]; headerImageView.layer.borderColor = [[UIColor greenColor] CGColor]; headerImageView.layer.borderWidth = 3; [self.view addSubview:headerImageView]; // 获取点击的图片 headerImageView.image = [info objectForKey:UIImagePickerControllerEditedImage]; } Swift
// 设置代理,需遵守 UIImagePickerControllerDelegate, UINavigationControllerDelegate 协议 imagePickerController.delegate = self // 设置是否需要做图片编辑 imagePickerController.allowsEditing = true // 判断设备数据来源是否支持 /* case PhotoLibrary // 来自图库 case Camera // 来自相机 case SavedPhotosAlbum // 来自相册 */ if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) { } // 判断摄像头是否支持 /* case Rear // 后置摄像头 case Front // 前置摄像头 */ if UIImagePickerController.isCameraDeviceAvailable(.Rear) { } // 判断闪光灯是否支持 if UIImagePickerController.isFlashAvailableForCameraDevice(.Front) { } // 获取数据来源支持的媒体类型 /* PhotoLibrary, // 来自图库 "public.image", "public.movie" Camera, // 来自相机 ... SavedPhotosAlbum // 来自相册 "public.image", "public.movie" */ let mediaTypesArray = UIImagePickerController.availableMediaTypesForSourceType(.SavedPhotosAlbum) // 获取相机拥有的模式 /* Rear, // 后置摄像头 ... Front // 前置摄像头 ... */ let captureModesArray = UIImagePickerController.availableCaptureModesForCameraDevice(.Front) // 设置数据来源 /* case PhotoLibrary // 来自图库 case Camera // 来自相机 case SavedPhotosAlbum // 来自相册 */ imagePickerController.sourceType = .PhotoLibrary // 设置媒体类型 /* kUTTypeImage 包含: kUTTypeImage; // 抽象的图片类型 kUTTypeJPEG; kUTTypeJPEG2000; kUTTypeTIFF; kUTTypePICT; kUTTypeGIF; kUTTypePNG; kUTTypeQuickTimeImage; kUTTypeAppleICNS; kUTTypeBMP; kUTTypeICO kUTTypeMovie 包含: kUTTypeAudiovisualContent; // 抽象的声音视频 kUTTypeMovie; // 抽象的媒体格式(声音和视频) kUTTypeVideo; // 只有视频没有声音 kUTTypeAudio; // 只有声音没有视频 kUTTypeQuickTimeMovie; kUTTypeMPEG; kUTTypeMPEG4; kUTTypeMP3; kUTTypeMPEG4Audio; kUTTypeAppleProtectedMPEG4Audio 需要 import MobileCoreServices 才能用 kUTTypeImage 和 KUTTypeMovie 。 default value is an array containing kUTTypeImage. */ imagePickerController.mediaTypes = [String(kUTTypeImage), String(kUTTypeMovie)] // 设置摄像头 /* Rear, // 后置摄像头,默认 Front // 前置摄像头 */ imagePickerController.cameraDevice = .Front // 设置相机模式 /* Photo, // 照相模式,默认 Video // 录像模式 */ imagePickerController.cameraCaptureMode = .Photo // 设置闪光灯模式 /* Off = -1, // 关 Auto = 0, // 自动,默认 On = 1 // 开 */ imagePickerController.cameraFlashMode = .On // 设置录像质量 /* TypeHigh = 0, // 高质量, highest quality TypeMedium = 1, // 中质量,默认, medium quality, Wi-Fi TypeLow = 2, // 低质量, lowest quality, cellular network Type640x480 = 3, // VGA 质量, VGA quality TypeIFrame1280x720 = 4, TypeIFrame960x540 = 5 apply only if mediaTypes includes kUTTypeMovie */ imagePickerController.videoQuality = .TypeHigh // 设置录像时间长度 /* default is 10 minutes. apply only mediaTypes includes kUTTypeMovie */ imagePickerController.videoMaximumDuration = 600 // 设置是否显示相机原生 UI /* set to NO to hide all standard camera UI. default is YES. available sourceType is Camera. */ imagePickerController.showsCameraControls = true // 自定义相机视图 /* set a view to overlay the preview view. default is YES. available sourceType is Camera. */ imagePickerController.cameraOverlayView = myView // 改变相机视图 /* set the transform of the preview view. default is YES. available sourceType is Camera. 旋转 45 度,需要输入的参数为弧度,45/180 * M_PI,1 度 = PI/180 弧度 */ imagePickerController.cameraViewTransform = CGAffineTransformMakeRotation(0.25 * CGFloat(M_PI)) // 打开相机/相册/图库 /* 访问设置的数据来源界面 */ self.presentViewController(imagePickerController, animated: true, completion: nil) // 拍照 /* 拍摄照片 */ imagePickerController.takePicture() // 录像 // 开始录像 imagePickerController.startVideoCapture() // 停止录像 imagePickerController.stopVideoCapture() // 退出当前界面(相机/相册/图库) /* 在 UIImagePickerControllerDelegate 协议方法中调用 */ picker.dismissViewControllerAnimated(true, completion: nil) // 获取选取媒体类型(图片或者视频) /* 选取的信息都在 info 中,info 是一个字典。字典中的键: UIImagePickerControllerMediaType; // 指定用户选择的媒体类型,包含着 kUTTypeImage 和 kUTTypeMovie UIImagePickerControllerOriginalImage; // 原始图片 UIImagePickerControllerEditedImage; // 修改后的图片,只有打开编辑模式 info 里才有此键值对 UIImagePickerControllerCropRect; // 裁剪尺寸,只有打开编辑模式 info 里才有此键值对 UIImagePickerControllerMediaURL; // 媒体的 URL UIImagePickerControllerReferenceURL; // 原件的 URL UIImagePickerControllerMediaMetadata // 当数据来源是照相机的时候这个值才有效 kUTTypeImage 包含: kUTTypeImage; // 抽象的图片类型 kUTTypeJPEG; kUTTypeJPEG2000; kUTTypeTIFF; kUTTypePICT; kUTTypeGIF; kUTTypePNG; kUTTypeQuickTimeImage; kUTTypeAppleICNS; kUTTypeBMP; kUTTypeICO kUTTypeMovie 包含: kUTTypeAudiovisualContent; // 抽象的声音视频 kUTTypeMovie; // 抽象的媒体格式(声音和视频) kUTTypeVideo; // 只有视频没有声音 kUTTypeAudio; // 只有声音没有视频 kUTTypeQuickTimeMovie; kUTTypeMPEG; kUTTypeMPEG4; kUTTypeMP3; kUTTypeMPEG4Audio; kUTTypeAppleProtectedMPEG4Audio 需要 import MobileCoreServices 才能用 kUTTypeImage 和 KUTTypeMovie 。 */ // 直接处理点击的媒体资源 let imageView = UIImageView(frame: CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20)) self.view.addSubview(imageView) // 获取点击的图片 imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage // 先判断点击的资源类型再处理 // 判断点击的媒体资源类型 if (info[UIImagePickerControllerMediaType]?.isEqualToString(String(kUTTypeImage)) != nil) { let headerImageView = UIImageView(frame: CGRectMake(20, 40, 100, 100)) headerImageView.layer.borderColor = UIColor.greenColor().CGColor headerImageView.layer.borderWidth = 3 self.view.addSubview(headerImageView) // 获取点击的图片 headerImageView.image = info[UIImagePickerControllerEditedImage] as? UIImage }
3、UIImagePickerControllerDelegate 协议方法
Objective-C
// 取消选择,点击界面中的取消(Cancel)按钮时触发 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { } // 选择完成,点击界面中的某个图片或者选择(Choose)按钮时触发 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { }
Swift
// 取消选择,点击界面中的取消(Cancel)按钮时触发 func imagePickerControllerDidCancel(picker: UIImagePickerController) { } // 选择完成,点击界面中的某个图片或者选择(Choose)按钮时触发 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { }