1

I am using this code to save the image in gallery and its working fine, the image is saved in simulator's gallery.

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        var myImage : UIImage = UIImage(named: "sample")!
        UIImageWriteToSavedPhotosAlbum(myImage, self, "image:didFinishSavingWithError:contextInfo:", nil)
    }

    func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
        if error == nil {
            let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert)
            ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
            presentViewController(ac, animated: true, completion: nil)
        } else {
            let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
            ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
            presentViewController(ac, animated: true, completion: nil)
        }
    }
}
  • How can I get the path of image which I just saved in gallery?
  • Then using that path, how do I get the image back from gallery and use it in my application?

I am using Xcode 7.3 ans swift 2.0 Thanks in advance.

pableiros
  • 14,932
  • 12
  • 99
  • 105
Umair Afzal
  • 4,947
  • 5
  • 25
  • 50

1 Answers1

0

You can use UIImagePickerController to select an image that has been saved to the gallery. The delegate function imagePickerController:didFinishPickingMediaWithInfo: will supply you with a dictionary containing information about the image that was chosen. The UIImage is contained in this dictionary under the UIImagePickerControllerOriginalImage key. You can read about the other keys available here.

Wyatt
  • 309
  • 2
  • 11
  • Thanks But I do not want to use UIImagePIckerController, because I want this process to happen programatically. Basically something like saving a profile picture and getting it when the user open the app next time. – Umair Afzal Jun 06 '16 at 18:01
  • Ah. Then when saving the image to the gallery you'll want to save it's filepath to another form of persistent storage, like a SQLite database or write it to NSUserDefaults like you mentioned in a comment earlier. To get the path back after you've saved it to NSUserDefaults, you'll need to save the path under a key that stays constant so you know where to look for it in the defaults dictionary. – Wyatt Jun 06 '16 at 18:09
  • what I am asking is how do I get the path of image I just saved ? and lets say on a button click get image back using that path ? saving the path for later user is another story – Umair Afzal Jun 06 '16 at 18:12
  • How are you choosing the image initially? – Wyatt Jun 06 '16 at 18:19
  • The Image will be coming from a chat – Umair Afzal Jun 06 '16 at 18:20
  • If it's coming in as a UIImage with no prior path, instead of using `UIImageWriteToSavedPhotosAlbum` you could manually write it to a filename of your choosing. There's an example of doing that as one of the answers to [this question](http://stackoverflow.com/questions/29009621/url-of-image-after-uiimagewritetosavedphotosalbum-in-swift). – Wyatt Jun 06 '16 at 18:24