0

I'm trying to create an app that are capable to take an image with the camera, load into the UIImageView. And when I restart the application, I want the photo to be loaded into the UIImageView, and how could I do that?

To take photo:

@IBOutlet weak var p2Image: UIImageView!    
@IBAction func p2takePhoto(_ sender: Any) {  

    let image = UIImagePickerController()  
    image.delegate = self  
    image.sourceType = UIImagePickerControllerSourceType.camera  
    image.allowsEditing = false  
    self.present(image, animated : true){}  

}  

After taking the photo, I display the photo with UIImageView and save the photo's path:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {  
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage  
    {  
        p2Image.image = image  
        UserDefaults.standard.set(p2Image.image?.accessibilityPath, forKey: "p2ImagePath")  
    }  
    else  
    {  

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

Codes to display my photo everytime I open my application:

override func viewDidAppear(_ animated: Bool) {  
    if let x = UserDefaults.standard.object(forKey: "p2ImagePath") as? String  
    {  
        p2Image.image = x  
    }  

But it din't work, can't even build, it says

p2Image.image = x (Cannot assign value of type string to type UIImage)

Am I missing something? Or am I using the wrong method?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
ramen87x
  • 160
  • 3
  • 18

2 Answers2

0

It's bad idea to store image inside UserDefaults; much better solution to store it on the disk

Here

if let x = UserDefaults.standard.object(forKey: "p2ImagePath") as? String  
{  
    p2Image.image = x  
} 

you try to convert something at path "p2ImagePath" to string, and if it succeeds, set it to your image view; if you want it to compile, cast it to UIImage instead, but, anyway, what you really should do is to store image as NSData at specific path and store it instead

EDITED:

I've missed your accessabilityPath and thought you are trying to store exactly image at the UserDefaults :) so, you can try to

if let x = UserDefaults.standard.object(forKey: "p2ImagePath") as? String  
{  
    p2Image.image = UIImage(contentsOfFile: x)
} 
Community
  • 1
  • 1
Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100
  • Hi there, thanks for replying. I have tried p2Image.image = UIImage(contentsOfFile: x). It have no error, and I can build it, but the image wasn't loaded into the app after I restarting it.. – ramen87x Nov 25 '16 at 03:46
  • The image doesn't show is it because I'm using simulator?? – ramen87x Nov 25 '16 at 08:42
  • I'm not sure, I've never used such approach with accessibilityPath; try to store it on the disk by yourself and than access it. There is link to answer, where described how to do this – Vladyslav Zavalykhatko Nov 25 '16 at 08:43
  • I'm confused with what I found, that why I thought I should give it a try by asking a question myself – ramen87x Nov 25 '16 at 16:20
0

I finally had solved with

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

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    {
        p2Image.image = image
        let saveImage = p2Image.image
        let imageData:NSData = UIImagePNGRepresentation(saveImage!)! as NSData

        UserDefaults.standard.set(imageData, forKey: "keyp2Image")
    }
    else
    {
        //error message
    }
    self.dismiss(animated: true, completion: nil)
}


override func viewWillAppear(_ animated: Bool) {


    if UserDefaults.standard.object(forKey: "keyp2Image") != nil
    {
    let data = UserDefaults.standard.object(forKey: "keyp2Image") as! NSData
    p2Image.image = UIImage(data: data as Data)
    }
}
ramen87x
  • 160
  • 3
  • 18