从IClo​​ud获取图像将冻结UI

时间:2018-03-03 16:22:07

标签: ios swift icloud phasset

我正在使用BSImagePicker lib在我的应用程序中选择多个图像,当用户选择存储在ICloud上的图像 UI将冻结在图像选择器控制器上,直到图像被下载我试图修复它但没有结果

这是我的代码:

选择图片并在滑块中显示

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


    }

返回所选资产图片的功能

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
    }

非常感谢任何帮助

2 个答案:

答案 0 :(得分:1)

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

您需要更新finish块中的代码,如下所示:

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

答案 1 :(得分:1)

尝试替换以下代码

for img in assets{

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

这一个:

for img in assets{
     let image = self.getUIImage(asset: img)!
     self.imagesource.append(ImageSource(image: image))
     self.imagesdata.append(image)
}