如何用AssetsPickerController iOS中的PHImageManager解决空资产问题? [英] How to fix empty asset issue with PHImageManager in AssetsPickerController iOS?

查看:101
本文介绍了如何用AssetsPickerController iOS中的PHImageManager解决空资产问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift中的AssetsPickerControllerDevice gallery中选择多个Videos.

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

问题:当我选择多个Videos或有时选择单个video时,有时我的App由于空的Video Asset而崩溃.在100次测试中,这种情况发生了5-10次.

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.

代码:

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

我竭尽全力在StackOverFlow中发现了类似的问题,并得到了一些建议,他们建议使用isNetworkAccessAllowed,但是在设置isNetworkAccessAllowed之后,我仍然没有得到Asset.

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.

推荐答案

首先,您不应该在此处forVideo: myPHAsset!

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

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

然后尝试将deliveryMode设置为自动:

Then try setting the deliveryMode to automatic:

options.deliveryMode = .automatic

要强制下载资产,我们需要指定版本:

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

options.version = .original

这篇关于如何用AssetsPickerController iOS中的PHImageManager解决空资产问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆