嗨,我正在创建一个相册应用程序作为一个学习项目,但我遇到了一些问题PHPhotoLibrary.shared().performChangesAndWait
;主要问题是当方法 saveImage 有PHPhotoLibrary.shared().performChangesAndWait
代码时;PHPhotoLibrary.shared().performChangesAndWait
该方法在完成保存图像之前退出。即使我使用performChangesAndWait
根据文档说它应该等到完成的方法,似乎代码也会异步执行。
这是我的类中 saveImage 方法的代码:
func saveImage(_ image: UIImage) ->Bool {
print("Executing saveImage")
var result:Bool = false
if assetCollection == nil {
print("the album is nil")
return result
}
print("saveImage: we will start saving the image")
//TODO: the return false is happening before the photo is save.
do
{
try PHPhotoLibrary.shared().performChangesAndWait({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
let enumeration: NSArray = [assetPlaceHolder!]
albumChangeRequest!.addAssets(enumeration)
print("saveImage: image was save without issues")
result = true
print("saveImage: result value after saving the image: \(result)")
})
}
catch let error
{
result = false
print("saveImage: there was a problem: \(error.localizedDescription)")
}
print("saveImage: result value before exiting the method: \(result)")
return result
}
基本上,当应用程序第一次运行时,返回结果在保存图像块完成之前执行。
有什么方法可以强制 saveImage 方法实际等到 performChangesAndWait 方法实际完成?
在此先感谢您的帮助。