问题
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