0
votes

User can save images on UIImageView into library using UIActivityViewController sheet and also share or post it into social media as seen below:

enter image description here

If user selects "Save Image", UIActivityViewController has no way i know to return saved image path. It will just save and dismiss silently.

How could i retrieve the path where it is saved? I will be storing the path to access saved images and display them in another view. Thank you very much for any help.

EDIT: Below is how i present UIActivityViewController when user tapped on share button.

let imageShare = [ self.imageView.image ]
let activityViewController = UIActivityViewController(activityItems: imageShare as [Any] , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.popoverPresentationController?.sourceRect = self.shareButton.frame

self.present(activityViewController, animated: true, completion: nil)
1
please add your code as well! How you are using UIActivityViewController?Daljeet
I added it, thank you for pointing out.Umit Kaya

1 Answers

1
votes

I think the question is: Since you already have the image... Why do you want to get that path?

However - you can get the selected "share" activity in the UIActivityViewController completion:

func shareImage(_ img: UIImage) -> Void {
    
    let items = [img]
    let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
    
    activityViewController.popoverPresentationController?.sourceView = self.view
    activityViewController.popoverPresentationController?.sourceRect = self.shareButton.frame

    activityViewController.completionWithItemsHandler = { (activityType: UIActivity.ActivityType?, completed:
                                        Bool, arrayReturnedItems: [Any]?, error: Error?) in
        if completed {
            if let act = activityType {
                switch act {
                case .saveToCameraRoll:
                    print("Save Image was selected")
                    // do something here
                    // such as getting the asset reference?
                    //  or getting the URL to the asset?
                
                case .copyToPasteboard:
                    print("Copy was selected")
                    
                default:
                    print("Something else...")
                }
            }
        } else {
            print("Cancel was selected")
        }
        if let shareError = error {
            print("error while sharing: \(shareError.localizedDescription)")
        }

    }
    
    present(activityViewController, animated: true)
    
}

So, depending on what you really want, you can easily find example code for getting the most recently saved image from the camera roll (or its asset reference, or URL, etc).