1
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    print("Image Uploaded")

    self.dismissViewControllerAnimated(true, completion: nil)

    filterimage.image = image   
}

@IBAction func importImage(sender: AnyObject) {
    let image = UIImagePickerController()
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.Camera

    image.allowsEditing = false

    self.presentViewController(image, animated: true, completion: nil)
}

After you close the app and you open it again, the image will disappear. How can I make it to when I open the app again the image stays there. I know how to do it with text with NSUserDefaults but how do you do it with an image?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    You can write an image data to a documents directory. – Evgeny Karkan Aug 09 '16 at 14:21
  • 1
    @TejaNandamuri Do not save the image in `NSUserDefaults`. Write the image as a file. If anything, you can store the filename of the save file in `NSUserDefaults`. – rmaddy Aug 09 '16 at 15:19

2 Answers2

1

You'll want to save it to the Documents directory like so:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    print("Image Uploaded")
    saveImageToDirectory(image)
    self.dismissViewControllerAnimated(true, completion: nil)  
}

func saveImageToDirectory(image : UIImage) {
    if let data = UIImageJPEGRepresentation(image, 1.0) {
        let filename = getDocumentsDirectory().stringByAppendingPathComponent("myCameraImage.png")
        data.writeToFile(filename, atomically: true)
    }
}

func getDocumentsDirectory() -> NSString {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

//To retrieve the image...
func getImage() {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
    let getImagePath = paths.stringByAppendingPathComponent("myCameraImage.jpg")
    let image = UIImage(contentsOfFile: getImagePath)
    print(image)
}

With credit to Hacking With Swift

BenJammin
  • 1,479
  • 13
  • 18
0

The editingInfo dictionary tells you the chosen image's source url (UIImagePickerControllerReferenceURL). Save off that information so that you can use it again later to fetch the same image.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • This solution doesn't deal with the user deleting the picture from the photo library. It would be better to save the image locally to the app, not just its URL. – rmaddy Aug 09 '16 at 15:17
  • @rmaddy And as you say, not in user defaults either. - I don't think my solution is invalid, though. If we are supposed to be displaying an image from the photo library, not displaying it if it isn't there might be the right thing to do. – matt Aug 09 '16 at 15:27