Ajp
Ajp

Reputation: 333

UIImagePickerController image in Swift

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)
}

Upvotes: 0

Views: 562

Answers (1)

toddg
toddg

Reputation: 2906

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)
    }
}

Upvotes: 1

Related Questions