0

I am using a UIImagePickerController in my program and it is effectively changing the image of an imageview i have added. However, whenever I restart this app and come back to the home screen, it is automatically resetting to the default image I had it to before, rather than the user selected image. How can I make it so that it records which image was last used, and reloads it every time the program starts?

 var imagePicker = UIImagePickerController()
 func chooseImage(_ sender: Any) { //function called with button press

    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.allowsEditing = true

    let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)

    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in

        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            imagePickerController.sourceType = .camera
            self.present(imagePickerController, animated: true, completion: nil)
        }else{
            print("Camera not available")
        }


    }))

    actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in
        imagePickerController.sourceType = .photoLibrary
        self.present(imagePickerController, animated: true, completion: nil)
    }))

    actionSheet.addAction(UIAlertAction(title: "Default", style: .default, handler: { (action:UIAlertAction) in
        self.avatarImageView.image = UIImage(named: "Avatar.png")


    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(actionSheet, animated: true, completion: nil)


}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let image = info[UIImagePickerControllerEditedImage] as! UIImage

    avatarImageView.image = image

    picker.dismiss(animated: true, completion: nil)



}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)
}
toddg
  • 2,863
  • 2
  • 18
  • 33
Ajp
  • 333
  • 1
  • 2
  • 9
  • I assume you mean an actual app restart (i.e. where it goes out of memory) rather than just tapping the home screen and then bringing the app back up? – toddg Apr 14 '17 at 19:14
  • 3
    You need to save your image somewhere and load it next time your app starts – Leo Dabus Apr 14 '17 at 19:21
  • @LeoDabus How can i do this? Kindly guide me. – Ajp Apr 14 '17 at 19:28
  • Save it as jpeg http://stackoverflow.com/questions/29726643/how-to-compress-of-reduce-the-size-of-an-image-before-uploading-to-parse-as-pffi/29726675#29726675 – Leo Dabus Apr 14 '17 at 19:29
  • 2
    You'll need to have some kind of persistence layer. You could use a database, Core Data or even NSUserDefaults. I'll try to put together an example later. – toddg Apr 14 '17 at 20:42

1 Answers1

1

Since the app is going out of memory, you'll need some kind of persistence mechanism for saving the image. The simplest way to do this would be to store the image in UserDefaults. This can be accomplished like this:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    let image = info[UIImagePickerControllerEditedImage] as! UIImage
    avatarImageView.image = image
    UserDefaults.standard.set(UIImagePNGRepresentation(image), forKey: "avatarImage")    

    picker.dismiss(animated: true, completion: nil)
}

Then when you reopen the app you'll need to check whether you've previously saved an avatarImage in UserDefaults and load it from there:

// Could be in viewDidLoad or wherever else you load your image
override func viewDidLoad() {

    if let imageData = UserDefaults.standard.object(forKey: "avatarImage") as? Data {
        avatarImageView.image = UIImage(data: imageData)
    }
}
toddg
  • 2,863
  • 2
  • 18
  • 33