iOS - UIImagePickerController
前言
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImagePickerController : UINavigationController <NSCoding>
@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 <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];
}
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]) { }
iOS - UIImagePickerController的更多相关文章
- ios UIImagePickerController简单说明
首先,VC中添加#import <MobileCoreServices/MobileCoreServices.h> 使用(NSString *) kUTTypeImage定义在其中 判断是 ...
- IOS UIImagePickerController(拍照或者读取相册)
UIImagePickerController ● 使用UIImagePickerController就可以进行拍照或者读取相册 ● 通过sourceType属性来决定拍照还是读取相册 ➢ UII ...
- ios uiimagepickercontroller 选择相册或者拍照上传
首先需要实现UIImagePickerControllerDelegate 代理 实现其imagePickerController 方法 这里用于选择图片或的拍照回调 //调用相机拍照 或者 图库选 ...
- IOS UIImagePickerController 保存图片到 相册
// 异步下载图片 dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0); disp ...
- IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序
前言:本篇博文是本人阅读国外的IOS Programming Tutorial的一篇入门文章的学习过程总结,难度不大,因为是入门.主要是入门UIImagePickerController这个控制器,那 ...
- iOS摄像头和相册-UIImagePickerController常用操作
在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频.并且从相册中选取我们需要的图片或者视频. 关于iOS摄像头和相册的应用,可以使用UIImagePickerController类来完成控制. ...
- IOS开发UI基础UIImagePickerController的属性
UIImagePickerController 1.+(BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType ...
- iOS开发—— UIImagePickerController获取相册和拍照
一.简单的拍照显示,或是从相册中直接选取照片 #import "ViewController.h" @interface ViewController ()<UIImageP ...
- 给iOS开发新手送点福利,简述UIImagePickerController的属性和用法
1.+(BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType; // 检查指定源是否在设备上 ...
随机推荐
- 13、mysql/触发器
1. mysql mysql基础 1)mysql存储结构: 数据库 -> 表 -> 数据 sql语句 2)管理数据库: 增加: create database 数据库 default ...
- HDU 3487:Play with Chain(Splay)
http://acm.hdu.edu.cn/showproblem.php?pid=3487 题意:有两种操作:1.Flip l r ,把 l 到 r 这段区间 reverse.2.Cut a b c ...
- PMO究竟啥样?(3)
PMO究竟啥样?(3) 继续上一篇,PMO究竟啥样?到这篇,这篇文章就完毕啦. 超卓基地COE,4大典型责任 我们知道全部的公司,它都是要不断地继续改善和优化,包括公司内安排级的项目处理的机制,也需求 ...
- js笔记---(运动)通用的move方法,兼容透明度变化
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Webstrom快捷键大全
20:32:59 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者 ) Shift+F6 重构-重命名 Ctrl+X ...
- 2015-09-17 001 日志与对话框公用类_public
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...
- Codeforces Round #336 Zuma
D. Zuma time limit per test: 2 seconds memory limit per test: 512 megabytes input: standard input ...
- Animation Play/Stop测试
测试结果: 1.当切换不同动画播放,可以直接调用Play或者CrossFade.会立即切过去 2.当相同动画再次播放,又想打断重头再播,需要先调用Stop. anim.Play("Test1 ...
- windows下wordpress环境快速搭建
所需要软件下载网址:https://bitnami.com/ 安装使用说明网址:http://www.websoft9.com/wp-content/plugins/documente/documen ...
- 同一服务器部署多个tomcat时的端口号修改详情
同一服务器部署多个tomcat时,存在端口号冲突的问题,所以需要修改tomcat配置文件server.xml,以tomcat7为例. 首先了解下tomcat的几个主要端口: <Connector ...