Swift 3 - How to load images into an IOS app

Hi there!


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 the 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?? Please help me..

That's logical, a string is not an image.


you should try something like p2image.image = UIImage(x)

Hi there, thanks for replying! I tried p2Image.image = UIImage(x), but it still shows me error message, this time is Swift Compiler Error: Argument labels '(_:)' do not match any available overloads. Any idea..?

UIImage(x) is just the pattern.


The exact call is UIImage(named: x) or UIImage(contentsOfFile: x)

I had tried both the method, both also no error, but it doesn't do the trick, is it because I'm using simulator? Since I'm using

UserDefaults.standard.set(p2Image.image?.accessibilityPath, forKey: "p2ImagePath"


I think is the best to use UIImage(contentsOfFile: x), right? By the way, .accessibilityPath is actually pointing at the image file's path, right? Or would it be better if i use document library??



Sorry for asking you this much question, I'm quite new to xcode..

Yes, you have to use I think is the best to use UIImage(contentsOfFile: x)


In the simulator you have access only to a small gallery of predefined images.


Can you try on an actual device ?

Yes, I had tried on an Iphone 5, it doesn't work, no error message, I do not know whats the problem..

Could you add a print statement:


- after

  UserDefaults.standard.set(p2Image.image?.accessibilityPath, forKey: "p2ImagePath") 
  Swift.print(p2Image.image?.accessibilityPath)

- and after

  p2Image.image = UIImage(contentsOfFile: x)
  Swift.print(x)


and tell what you get (on simulator)

objc[1359]: Class PLBuildVersion is implemented in both 
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/
System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x12aec9998) and 
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk
/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x129dc3d38). 
One of the two will be used. Which one is undefined.
2016-11-26 00:45:15.039905 myproject[1359:38345] 
[Generic] Creating an image format with an unknown type is an error
nil

I 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
        {
            /
        }
        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)
        }
    }
Swift 3 - How to load images into an IOS app
 
 
Q