gpt4 book ai didi

ios - 从 iCloud 获取图像会卡住 UI

转载 作者:搜寻专家 更新时间:2023-11-01 07:04:08 26 4
gpt4 key购买 nike

我正在使用 BSImagePicker 库在我的应用程序中选择多个图像,当用户选择存储在 ICloud 上的图像时,用户界面将卡住在图像选择器 Controller 上,直到图像被下载我尝试修复它但没有结果

这是我的代码:

选择图片并在 slider 中显示它们

 @IBAction func choose_images(_ sender: Any) {

bs_presentImagePickerController(vc, animated: true,
select: { (asset: PHAsset) -> Void in
// User selected an asset.
// Do something with it, start upload perhaps?
}, deselect: { (asset: PHAsset) -> Void in
// User deselected an assets.
// Do something, cancel upload?
}, cancel: { (assets: [PHAsset]) -> Void in

}, finish: { (assets: [PHAsset]) -> Void in
// User finished with these assets

// print(assets.count)

DispatchQueue.main.async{
self.imagesource.removeAll()
self.imagesdata.removeAll()
for img in assets{

self.imagesource.append(ImageSource(image: self.getUIImage(asset: img)!))
self.imagesdata.append(self.getUIImage(asset: img)!)
}

self.slider.setImageInputs(self.imagesource)
self.slider.contentScaleMode = UIViewContentMode.scaleAspectFill
self.slider.slideshowInterval = 2
self.slider.zoomEnabled = true
self.slider.backgroundColor = UIColor.black
self.slider.pageControlPosition = PageControlPosition.insideScrollView
let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.didTap))
self.slider.addGestureRecognizer(recognizer)
}
}, completion: nil)


}

返回所选 Assets 图像的函数

func getUIImage(asset: PHAsset) -> UIImage? {

var img: UIImage?
let manager = PHImageManager.default()
let options = PHImageRequestOptions()
options.version = .original
options.isNetworkAccessAllowed = true
options.isSynchronous = true
options.progressHandler = { (progress, error, stop, info) in
if(progress == 1.0){
SVProgressHUD.dismiss()
} else {

SVProgressHUD.showProgress(Float(progress), status: "Downloading from iCloud")

}
}
manager.requestImage(for: asset, targetSize:PHImageManagerMaximumSize, contentMode: PHImageContentMode.aspectFit, options: options) { (image, info) in

img = image
}

return img
}

任何帮助将不胜感激

最佳答案

您的主要问题是您正在同步访问主队列中的远程图像。您需要在后台执行循环。

您需要按如下方式更新 finish block 中的代码:

}, finish: { (assets: [PHAsset]) -> Void in
DispatchQueue.global().async {
self.imagesource.removeAll()
self.imagesdata.removeAll()
for img in assets {
if let res = getUIImage(asset: img) {
self.imagesource.append(ImageSource(image: res))
self.imagesdata.append(res)
}
}

DispatchQueue.main.async {
self.slider.setImageInputs(self.imagesource)
self.slider.contentScaleMode = UIViewContentMode.scaleAspectFill
self.slider.slideshowInterval = 2
self.slider.zoomEnabled = true
self.slider.backgroundColor = UIColor.black
self.slider.pageControlPosition = PageControlPosition.insideScrollView
let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.didTap))
self.slider.addGestureRecognizer(recognizer)
}
}

您可能还必须更新 getUIImage 中的进度处理程序,以更新主队列上的 SVProgressHUD。

请注意,对于上面的代码,除了我刚才提到的一个更改外,您应该保持 getUIImage 不变。

关于ios - 从 iCloud 获取图像会卡住 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com