穿越吧
解决iOS11,UIImagePickerController没有提示请求权限的问题
pod 'YVImagePickerController' ,'~> 1.0.7'
2.在plist文件中加入
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能访问相册</string>
import Photos
遵守协议 YVImagePickerControllerDelegate
初始化
let pickerVC = YVImagePickerController()
self.present(pickerVC, animated: true, completion: nil)
配置
yvmaxSelected | 多选时最大张数 |
---|---|
yvcolumns | 每行列数 |
topViewColor | 导航栏背景色 |
yvmediaType | 媒体类型:照片或视频 |
yvIsMultiselect | 是否多选,默认单选 |
selectedBtn_nimage | 多选时,未选中image |
selectedBtn_simage | 多选时,选中image |
实现代理方法
func yvimagePickerController(_ picker: YVImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { }
func yvimagePickerControllerDidCancel(_ picker: YVImagePickerController) {}
4.示例
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
/*
照片
每行4列
单选
*/
let pickerVC = YVImagePickerController()
pickerVC.yvmediaType = .image
pickerVC.yvcolumns = 4
pickerVC.yvIsMultiselect = false
pickerVC.delegate = self
self.present(pickerVC, animated: true, completion: nil)
/*
照片
每行5列
多选
*/
let pickerVC = YVImagePickerController()
pickerVC.yvmediaType = .image
pickerVC.yvcolumns = 5
pickerVC.yvIsMultiselect = true
pickerVC.delegate = self
self.present(pickerVC, animated: true, completion: nil)
/*
视频
每行5列
单选
*/
let pickerVC = YVImagePickerController()
pickerVC.yvmediaType = .video
pickerVC.yvcolumns = 5
pickerVC.yvIsMultiselect = false
pickerVC.delegate = self
self.present(pickerVC, animated: true, completion: nil)
}
func yvimagePickerController(_ picker: YVImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//单选照片
if info["imagedata"] != nil{
let image = info["imagedata"] as! UIImage
}
//单选视频
else if info["videodata"] != nil{
let url = info["videodata"] as! URL
}
//多选照片
else if info["imagedatas"] != nil{
let phassets = info["imagedatas"] as! Array<PHAsset>
}
}
func yvimagePickerControllerDidCancel(_ picker: YVImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}