3

嗨,我正在创建一个相册应用程序作为一个学习项目,但我遇到了一些问题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 方法实际完成?

在此先感谢您的帮助。

4

2 回答 2

4

performChangesAndWait是一个异步回调方法。result在inside上所做的更改performChangesAndWait不会反映在 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)
    }
}

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

于 2017-02-23T05:27:31.347 回答
0

这是解决方案(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...")
        }
    }
}
于 2020-04-19T12:52:56.617 回答