2

I'm building an app where I let the user to pick an image from its photo library. I was using this code to save it.

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    imgPicker.dismissViewControllerAnimated(true, completion: nil)
    NSUserDefaults.standardUserDefaults().setValue(image, forKey: "bgImage")
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    imgPicker.dismissViewControllerAnimated(true, completion: nil)
}

I later figured out that you can't save the image with NSUserDefaults, but you can save the picked image's path, save it and load it with "if let..."

But I don't know how to do this (find the path and save it). The idea is for the user to choose the view background image, kinda like how Whatsapp does.

If I'm wrong and you can't store the path, is there any easy way to save the picked image without using online servers?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Henrique Dourado
  • 320
  • 1
  • 7
  • 16

2 Answers2

10

You can save and retrieve it using NSUserDefaults:

//Save image
let img = UIImage() //Change to be from UIPicker
let data = UIImagePNGRepresentation(img)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "myImageKey")
NSUserDefaults.standardUserDefaults().synchronize()

//Get image
if let imgData = NSUserDefaults.standardUserDefaults().objectForKey("myImageKey") as? NSData {
    let retrievedImg = UIImage(data: imgData)
}

Or you can read / write to file, if you prefer:

//Save image
let img = UIImage() //Change to be from UIPicker
let data = UIImagePNGRepresentation(img)!
do {
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
    try data.writeToFile("\(documentsPath)myImage", options: [])

} catch {
    print("Error")
}

//Get image
do {
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
    let readData = try NSData(contentsOfFile: "\(documentsPath)myImage", options: [])
    let retreivedImage = UIImage(data: readData)
}
catch {
    print("Error")
}

Swift 3:

//Save image
let data = UIImagePNGRepresentation(pickedImage)!
do {
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    try data.write(to: URL(string: "\(documentsPath)/myImage")!, options: .atomic)
} catch {
    print("Error")
}

//Get image
do {
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    let readData = try Data(contentsOf: URL(string: "\(documentsPath)/myImage")!)
    let retreivedImage = UIImage(data: readData)
} catch {
    print("Error")
}
brimstone
  • 3,370
  • 3
  • 28
  • 49
  • But will the first method really save the picked image and I can load it everywhere? If so, I'll use the smaller one, cause the image is just used once in my app – Henrique Dourado Feb 28 '16 at 18:02
  • @HenriqueDourado The first one and the second one can be used anywhere in your app. I would use the first one personally because it is simpler and you don't really need to write to files for such a small amount of data. Btw both methods can be accessed even if the app is closed and reopened – brimstone Feb 28 '16 at 18:15
  • 3
    It's not appropriate to store data, especially image data, in `NSUserDefaults`. – rmaddy Feb 28 '16 at 23:00
0

There are two ways, one that you are describing but that is quite messy, the one I would suggest you is to take the image, turn into image data and store it in your application locally like in a sqlite database or in an array in user default. However saving that much data in user default is not a good practise. So, I would go with store it locally and I won't do it with messy sqlite codes, rather I would do it with core data.

class AddImageViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet var popUpThePickerButton: UIButton! //click on this button to appear the imagePicker
    @IBOutlet var addPictureButton: UIButton! //when this button clicked saveImage method is called and there you save the imageData wherever you want

    let picker = UIImagePickerController()
    var selectedImage : UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()
        picker.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // when popUpThePickerButton is clicked
    @IBAction func selectPictureFromPhotos(sender: UIButton) { 
        picker.editing = false
        picker.sourceType = .PhotoLibrary

        presentViewController(picker, animated: true, completion: nil)
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        self.selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage 
        dismissViewControllerAnimated(true, completion: nil)
    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
    }

// then save this image data in your database and when you want to show that image you can just turn the image data to image again

    @IBAction func saveImage(sender: UIButton) {
        let imageData = UIImagePNGRepresentation(self.selectedImage)
        //save this image data in database
    }
}
Natasha
  • 6,651
  • 3
  • 36
  • 58
  • But in the function "saveImage" you just assigned a constant to the image, but didn't save it! – Henrique Dourado Feb 28 '16 at 18:01
  • Yes, I believe, stack suggests, author to try some stuffs on his part too. I gave you a pretty good head start. Now it is unto you where you want o save it. In a sqlite database or in user default as you were initially trying. – Natasha Feb 29 '16 at 14:43
  • Also try to follow the suggestions too. Don't just look at the code. I suggested using core data. Showing how to save something in core would require me to show you how to create a core data model and entity, then how to create NSManagedObject and all which you should look up in a core data tutorial. You shouldn't want someone to write the code for you. Please take it as a constructive suggestion. – Natasha Feb 29 '16 at 14:54