swift3.0调用相册

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)
}
}

6.打印信息

swift3.0调用相册

上一篇:详解mybatis配置文件


下一篇:剑指offer——python【第16题】合并两个有序链表