How to fix empty asset issue with PHImageManager in AssetsPickerController iOS?

一个人想着一个人 提交于 2019-12-11 23:54:42

问题


I am working on AssetsPickerController in Swift to select multiple Videos from Device gallery.

Problem: When I am selecting multiple Videos or sometimes single video then sometimes my App is crashing due to Empty Video Asset. This is happening 5-10 times, out of 100 times of testing.

Code:

@IBAction func openAssetsAction(_ sender: UIButton) {
        let rootListAssets = AssetsPickerController()
        rootListAssets.didSelectAssets = {(assets: Array<PHAsset?>) -> () in

            for i in 0..<assets.count {

                let myPHAsset = assets[i]

                let options = PHVideoRequestOptions()
                options.deliveryMode = .highQualityFormat
                options.isNetworkAccessAllowed = true

                options.progressHandler = {  (progress, error, stop, info) in
                    print("progress: \(progress)")
                }

                PHImageManager.default().requestAVAsset(forVideo: myPHAsset!, options: options, resultHandler: { (asset, audioMix, info) in
                    if let urlAsset = asset as? AVURLAsset {
                        let localVideoUrl = urlAsset.url
                        print(localVideoUrl)
                    }
                })
            }
        }

        let navigationController = UINavigationController(rootViewController: rootListAssets)
        present(navigationController, animated: true, completion: nil)
    }

I checked my best to find the similar issue in StackOverFlow and got some, they are suggesting to use isNetworkAccessAllowed, but after setting isNetworkAccessAllowed, still I am getting nil Asset.


回答1:


First, you shouldn't force unwrap the asset here forVideo: myPHAsset!

guard 
  let myPHAsset = assets[i] 
else { 
  return 
}

Then try setting the deliveryMode to automatic:

options.deliveryMode = .automatic

EDIT:

To force the download of the asset, we need to specify the version:

options.version = .original


来源:https://stackoverflow.com/questions/55488998/how-to-fix-empty-asset-issue-with-phimagemanager-in-assetspickercontroller-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!