前言
- iOS 获取图片有三种方法:
- 直接调用摄像头拍照;
- 从相册中选择;
- 从图库中选择。
- UIImagePickerController 是系统提供的用来获取图片和视频的接口。用 UIImagePickerController 类来获取图片视频,大体分为以下几个步骤:
- 初始化 UIImagePickerController 类;
- 设置 UIImagePickerController 实例的数据来源类型;
- 设置代理;
- 如果需要做图片修改的话设置 allowsEditing = YES。
1、imagePickerController 的创建
- 需遵守协议 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];
}
2、imagePickerController 的设置
// 设置代理,需遵守 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 <MobileCoreServices/MobileCoreServices.h> 才能用 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 <MobileCoreServices/MobileCoreServices.h> 才能用 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];
}
3、UIImagePickerControllerDelegate 协议方法
// 取消选择,点击界面中的取消(Cancel)按钮时触发
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
}
// 选择完成,点击界面中的某个图片或者选择(Choose)按钮时触发
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
}