第一次 运行 时,在自定义相册中保存照片失败

Saving a photo in a custom album fails when run for the first time

你好,我正在创建一个相册应用程序作为一个学习项目,但我在使用 PHPhotoLibrary.shared().performChangesAndWait 时遇到了一些问题;主要问题是当方法 saveImage 具有 PHPhotoLibrary.shared().performChangesAndWait 代码时;该方法在 PHPhotoLibrary.shared().performChangesAndWait 完成保存图像之前退出。似乎代码会异步执行,即使我使用 performChangesAndWait 方法,根据文档说它应该等到它完成。

这里是我的class中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

}

基本上,当应用程序第一次运行时,return 结果在保存图像块完成之前再次执行。

有什么方法可以强制 saveImage 方法实际等待直到 performChangesAndWait 方法实际完成?

在此先感谢您的帮助。

performChangesAndWait 是一个异步回调方法。在 performChangesAndWait 中对 result 所做的更改不会反映在 return 上,因为这两个在不同的线程中。

等待 saveImage 方法完成的一种方法是实现回调。类似于:

func saveImage(_ image: UIImage, callback: @escaping(_ result: Bool)->Void) {
    print("Executing saveImage")

    if assetCollection == nil {
        print("the album is nil")
        callback(false)
    }
    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")

            callback(true)
        })
    }
    catch let error {
        print("saveImage: there was a problem: \(error.localizedDescription)")
        callback(false)
    }
}

让我们知道你得到了什么。

这是解决方案 (Swift 5):

@objc private func handleSavePhoto() {

    guard let previewImage = self.photoImageView.image else { return }

    PHPhotoLibrary.requestAuthorization { (status) in
        if status == .authorized {
            do {
                try PHPhotoLibrary.shared().performChangesAndWait {
                    PHAssetChangeRequest.creationRequestForAsset(from: previewImage)
                }
            } catch let error {
                print("failed to save photo in library: ", error)
            }
        } else {
            print("Something went wrong with permission...")
        }
    }
}