调起相册
let imgSelectVC = TZImagePickerController(maxImagesCount: 1, columnNumber: 4, delegate: self, pushPhotoPickerVc: true)
imgSelectVC?.allowPickingOriginalPhoto = true
imgSelectVC?.allowPickingVideo = false
imgSelectVC?.allowPickingGif = false
imgSelectVC?.allowTakeVideo = false
imgSelectVC?.showSelectBtn = true
imgSelectVC?.allowTakePicture = false
weakSelf.present(imgSelectVC!, animated: true, completion: nil)
获取原图方法: -> 代理方法:
func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool) {
if isSelectOriginalPhoto{
//选择了原图
guard let assets = assets as? [PHAsset] else {
return
}
/// 初始化信号量为1,最大并发为1,为0时会等待
let semap = DispatchSemaphore.init(value: 0)
var source: [YPQuickImageCCellVM] = []
DispatchQueue.main.async {
YPProgressHUD.show()
}
DispatchQueue.global().async {
let group = DispatchGroup()
let queue = DispatchQueue.global()
assets.forEach { asset in
queue.async(group: group, execute: {
})
}
assets.forEach { asset in
let item = YPQuickImageCCellVM.init(image: nil, asset: asset)
TZImageManager.default().getOriginalPhoto(with: asset) { image, info in
/// 是否是低清图
guard let isThumb = info?["PHImageResultIsDegradedKey"] as? Bool else{ return }
if isThumb{
//缩略图
item.image = image
}else{
//高清图
item.original = image
semap.signal()
}
}
semap.wait()
source.append(item)
}
DispatchQueue.main.async {[weak self] in
YPProgressHUD.hide()
guard let weakSelf = self else {return}
var dataSource: [YPQuickImageCCellVM] = source
if source.count < 3,
let image = UIImage.init("image_updat"){
dataSource.append(.init(default: image))
}
weakSelf.dataSource.accept(dataSource)
}
}
}else{
let source = photos.enumerated().map{ photo -> YPQuickImageCCellVM in
return YPQuickImageCCellVM.init(image: photo.element, asset: assets.safe(idx: photo.offset) as? PHAsset)
}
var dataSource: [YPQuickImageCCellVM] = source
if source.count < 3,
let image = UIImage.init("image_updat"){
dataSource.append(.init(default: image))
}
self.dataSource.accept(dataSource)
}
}
}
主要是通过 TZImageManager.default().getOriginalPhoto(with: asset)方法获取原图. 注意的是需要到异步线程操作.