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?