我正在尝试将视频保存到Gallery,之后检索资产,在ios 12中运行良好,现在在ios 13中测试它不再运行。视频已正确保存,但检索到的资源不是正确的,而是库中的另一个。
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: filePath)
}) { completed, error in
if completed {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions).lastObject
GeneralUtils().shareVideoFacebook(fetchResult!, self)
}
}
我刚刚找到了这个问题的解决方案。您可以要求提供已保存图像的本地标识符。
var id: String?
PHPhotoLibrary.shared().performChanges({
let collection = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: filePath)
id = collection?.placeholderForCreatedAsset?.localIdentifier
}) { completed, error in
if completed {
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [id!], options: .none).firstObject
GeneralUtils().shareVideoFacebook(fetchResult!, self)
}
}
DispatchQueue.global(qos: .background).async {
if let url = URL(string: url),
let urlData = NSData(contentsOf: url)
{ let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let filePath="(documentsPath)/file.mp4"
DispatchQueue.main.async {
urlData.write(toFile: filePath, atomically: true)
PHPhotoLibrary.shared().performChanges({
self.instagramVideoId = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: filePath))?.placeholderForCreatedAsset?.localIdentifier
}) { completed, _ in
if completed {
let url = URL(string: "instagram://library?LocalIdentifier=(self.instagramVideoId!)")!
DispatchQueue.main.async {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
} else {
}
}
}
}
}
}
self.hideLoader()
}