提醒一点。UIImagePickerController继承于UINavigationController所以在调用代理方法时,应同时遵循两个代理协议。另外,它还可以用于拍摄视频。
官网文档:UIImagePickerController Class Reference UIImagePickerControllerDelegate
1、 初始化UIImagePickerController类三种数据来源:
2、 设置其实例的数据源
3、 设置其实例的代理方法(使用的文件应遵守两个代理协议)
4、 设置图片是否可被编辑 .allowsEditing = false/true 默认false
5、 设置其他属性,(跳转到相册或者相机选择图片)
6、 完成图选择后回掉代理方法【重点】这样才可以回去到图片数据
UIImagePickerControllerSourceTypePhotoLibrary, // 来自图库二. 使用UIImagePickerController创建一个照片选择器
UIImagePickerControllerSourceTypeCamera, // 来自相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 来自相册
注意:开发的时候需要在info中添加相册访问权:Privacy - Photo Library Usage Description
和相机访问权:Privacy - Camera Usage Description
否则无法使用
import UIKit
protocol PhotoPickerViewControllerDelegate {
func getImageFromPicker(image:UIImage)//传回照片
}
class PhotoPickerViewController: UIViewController ,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
var alert:UIAlertController? //一个弹出提示框
var picker:UIImagePickerController? //放照片的选择器
var delegate:PhotoPickerViewControllerDelegate!
init(){
super.init(nibName: nil, bundle: nil)
self.modalPresentationStyle = .OverFullScreen //[learn]这样可以设置它的透明,看到上一个视图
self.view.backgroundColor = UIColor.clearColor()
self.picker = UIImagePickerController()
self.picker?.allowsEditing = false //设置照片不可编辑
self.picker?.delegate = self //注意选择器的代理遵守两个
}
/**
调用视图出现后,要显示的哪个对话框(注意对话框一般是放在某个动作发生后)
*/
override func viewDidAppear(animated: Bool) { //对话框的设置
if alert == nil {
alert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
self.alert?.addAction(UIAlertAction(title: "从相册选择", style: UIAlertActionStyle.Default, handler: { (action) in self.loadPhoto()
}))
self.alert?.addAction(UIAlertAction(title: "打开相机", style: UIAlertActionStyle.Default, handler: { (action) in self.takePhoto()
}))
self.alert?.addAction(UIAlertAction(title: "取消", style: .Cancel, handler: { (action) in
self.dismissViewControllerAnimated(true, completion: nil)//注意这句非常重要,否则无法与上一个界面交互
}))
self.presentViewController(self.alert!, animated: true, completion: nil) //注意相似的东西有好多个了,注意使用的是函数
}
}
/**
从相册选择
*/
func loadPhoto(){
self.picker?.sourceType = .PhotoLibrary //将选择器设置为相册
self.presentViewController(self.picker!, animated: true) {
}
}
/**
打开相机
调用相机,首先许哟判断相机存在否
*/
func takePhoto(){
if UIImagePickerController.isSourceTypeAvailable(.Camera){//[learn] 判断相机是否存在
self.picker?.sourceType = .Camera //将选择器的资源类型设置为相机
self.presentViewController(self.picker!, animated: true, completion: { //将当前的控制器转向相机
})
}
else{
let alertView = UIAlertController(title: "该机型无相机", message: nil, preferredStyle: .Alert)//这个ios9.0已经弃用了,要求识用那个UIalertController
alertView.addAction(UIAlertAction(title: "取消", style:.Cancel, handler: { (action) in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alertView, animated: true, completion: {
})//显示对提示框
}
}
//取消选择照片后的回掉
//取消照片选择器选择(返回上一个控制器)
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
self.picker?.dismissViewControllerAnimated(true, completion: { //去掉照片相册控制器
self.dismissViewControllerAnimated(true, completion: { //去本选择器控制器,回到哪个调用的哪个控制器
})
})
}
//选择照片回掉
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage //获取到选择器的原始图片
self.picker?.dismissViewControllerAnimated(true, completion: {
self.dismissViewControllerAnimated(true) {
self.delegate.getImageFromPicker(image) //调用代用将图片传回
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
}
required init?(coder aDecoder: NSCoder) {
fatalError("初始化构造器没有实例")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
三、其他的特别需求设置
1.调用前摄像头(默认调用后摄像头)
//如果有前置摄像头则调用前置摄像头
if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front){
picker.cameraDevice = UIImagePickerControllerCameraDevice.Front
}
2、设置闪光灯//开启闪光灯
3.选择回掉中info参数解释picker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On
此处参数 info 是一个字典,下面是字典中的键值 (从相机获取的图片和相册获取的图片时,两者的info值不尽相同)
UIImagePickerControllerMediaType; // 媒体类型
UIImagePickerControllerOriginalImage; // 原始图片
UIImagePickerControllerEditedImage; // 裁剪后图片
UIImagePickerControllerCropRect; // 图片裁剪区域(CGRect)
UIImagePickerControllerMediaURL; // 媒体的URL
UIImagePickerControllerReferenceURL // 原件的URL
UIImagePickerControllerMediaMetadata // 当数据来源是相机时,此值才有效
4.将拍照后的照片放在相册中
UIImageWriteToSavedPhotosAlbum ()
四、使用VPImageCropperViewController剪切图片
在github上下载VPImageCropperVieController,将.h和.m添加到桥接文件即可使用
使用时候记得实现代理方法回掉(截取和取消)
不过我用的时候会发生图片与屏幕不贴合????带解决
//获取图片
func getImageFromPicker(image: UIImage) {
var rect = CGRect(x: 0, y: 100, width: SCREEN_WINDTH, height: SCREEN_HEIGHT*1.2)
var vpc = VPImageCropperViewController(image: image, cropFrame: rect, limitScaleRatio: 2)
vpc.delegate = self
self.presentViewController(vpc, animated: true) {
}
}
/**
实现代理方法VPImageCropperDelegate
*/
func imageCropper(cropperViewController: VPImageCropperViewController!, didFinished editedImage: UIImage!) {
cropperViewController.dismissViewControllerAnimated(true) {
self.bookTitle?.faceButton?.setImage(editedImage, forState: .Normal)
}
}
func imageCropperDidCancel(cropperViewController: VPImageCropperViewController!) {
cropperViewController.dismissViewControllerAnimated(true) {
}
}