UIImageWriteToSavedPhotosAlbum unreliable

UIImageWriteToSavedPhotosAlbum is not working reliably for me.

It seems to work about a third of the times I call it.

When I say does not work, I mean the callback is not called, and it silently returns

When I use the code snipped below I get the results:

Call UIImageWriteToSavedPhotosAlbum
Called UIImageWriteToSavedPhotosAlbum

If I call it twice in a row sometimes the first call works, never the second.

Neither of the print statements in the callback are executed

class ImageSaver: NSObject {
    func writeToPhotoAlbum(image: UIImage) {
        print("Call UIImageWriteToSavedPhotosAlbum")
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
        print("Called UIImageWriteToSavedPhotosAlbum")
    }

    // https://www.hackingwithswift.com/example-code/media/uiimagewritetosavedphotosalbum-how-to-write-to-the-ios-photo-album
    @objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
        if let error = error {
            // we got back an error!
            print("Error: \(error.localizedDescription)")
        } else {
            print ("Image saved")
        }
    }
}

Do I need to use a semephore to serialise the callback? If I call UIImageWriteToSavedPhotosAlbum twice without waiting for the callback to complete will the second call fail silently?

This is ongoing.

I simplified the code to:

import UIKit
func SaveImageAsPhoto(image:UIImage) {
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}

and I get the same results. At best it works half the time.

UIImageWriteToSavedPhotosAlbum unreliable
 
 
Q