swift3.0调用相册首先需要注意:
1.swift3.0中调用相机和相册会导致崩溃
1.需要在
info.plist
文件中加入两个键值对,如下:
/// 都是String类型,内容任意的字符串即可
Privacy - Photo Library Usage Description
Privacy - Camera Usage Description
2.UIImagePickerControllerSourceType
枚举
public enum UIImagePickerControllerSourceType : Int {
case photoLibrary //相册
case camera //相机
case savedPhotosAlbum //保存照片专辑
}
3.功能实现Demo
//FIXME: 点击添加图片按钮
@objc fileprivate func AddPhotoClick(){
//1.判断照片控制器是否可用 ,不可用返回
if !UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
return
}
//2.创建照片控制器
let picVc = UIImagePickerController()
//3.设置控制器类型
picVc.sourceType = .photoLibrary
//4.设置是否可以管理已经存在的图片或者视频
picVc.allowsEditing = true
//5.设置代理
picVc.delegate = self
//6.弹出控制器
present(picVc, animated: true, completion: nil)
}
4.UIImagePickerController
协议
1.需要遵循
2个
协议:
UIImagePickerControllerDelegate
UINavigationControllerDelegate
5.常用imagePickerController代理
方法
// MARK:- UIImagePickerController代理方法
extension LXRComposeViewController : UIImagePickerControllerDelegate,UINavigationControllerDelegate{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
LXRLog(message: info)
}
}