0

I am running code UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) to save a photo(which is a image from a URL) to the album library. This works perfectly. However, This is a blind function and I would like to call a callback to know if it saved successfully.

Here is what I am trying.

            UIImageWriteToSavedPhotosAlbum(image, self, #selector(imageSaved(_:didFinishSavingWithError:contextInfo:)), nil)

            func imageSaved(image: UIImage!, didFinishSavingWithError error: NSError?, contextInfo: AnyObject?) {
                if (error != nil) {
                    print("error")
                    print(error)
                    //Do Something with error associated with image
                } else {
                    // Everything is alright.
                }
            }

I am trying to work with this code here and I keep getting Use of unresolved identifier imageSaved(_:didFinishSavingWithError:contextInfo:)

My problem is I don't understand the completion selector, and what is going on from Apple Documentation

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;

What does that ^ mean? How do I work with that?

Ryan B.
  • 99
  • 2
  • 14

3 Answers3

3

Try this:

UIImageWriteToSavedPhotosAlbum(image, self, #selector(imageSaved(image:didFinishSavingWithError:contextInfo:)), nil)

Hope it helps!!

Agent Smith
  • 2,873
  • 17
  • 32
  • OH! Almost worked. Then the I try, here is what I get: does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -["AppName.FirebaseHelper methodSignatureForSelector:] 2018-02-20 06:46:43.445648-0800 "AppName"[7749:4189179] Unrecognized selector -AppName.FirebaseHelper methodSignatureForSelector:] – Ryan B. Feb 20 '18 at 14:48
  • FYI, Idk if this helps, but I am testing to know what happens if the user didn't allow the app to access their photo albums. Then handle an error letting them know why they don't have the image. – Ryan B. Feb 20 '18 at 14:49
  • @RyanB. Is this a `runtime` error or `compile time`? – Agent Smith Feb 20 '18 at 14:50
  • It’s a runtime error when I click “download” and the functions get called – Ryan B. Feb 20 '18 at 14:50
  • 1
    @RyanB. Then it's related to something else. Although if you want to check for the permissions [check this link](https://stackoverflow.com/questions/26595343/determine-if-the-access-to-photo-library-is-set-or-not-phphotolibrary) and after checking call your function – Agent Smith Feb 20 '18 at 14:53
1

I had a similar issue which I solved by making the class inherit from NSObject.

aiden
  • 21
  • 2
  • actually, I tried every combination of the handlers i found in multiple threads and nothing worked until i tried the inherit from NSObject approach, and then, suddenly no error anymore -> +1 I have to say, that i used the call in a singleton, which had no inheritance before – Vario Sep 23 '19 at 06:16
0

As mentioned in Apple's Documentation here: UIImageWriteToSavedPhotosAlbum(::::)

completionSelector The method selector of the completionTarget object to call. This optional method should conform to the following signature:

- (void)image:(UIImage *)image
    didFinishSavingWithError:(NSError *)error
                 contextInfo:(void *)contextInfo;

So your callback function in Swift must be something like

@objc func imageSaved(image:UIImage,didFinishSavingWithError error:Error,contextInfo:UnsafeMutableRawPointer?){
        self.showAlert(title:"Image saved to Photos!", message: "");
    }
Sandeep Rana
  • 3,251
  • 2
  • 24
  • 38