我正在AssetsPickerController
的Swift
工作,从Videos
中选择多个Device gallery
。
问题:当我选择多个Videos
或有时单个video
时,有时我的App
因空Video Asset
而崩溃。在100次测试中,这种情况发生了5-10次。
码:
@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
。
首先,你不应该强迫在这里打开资产forVideo: myPHAsset!
guard
let myPHAsset = assets[i]
else {
return
}
然后尝试将deliveryMode设置为自动:
options.deliveryMode = .automatic
编辑:
要强制下载资产,我们需要指定版本:
options.version = .original