iosuiimageviewuiimageswift4uiimagepngrepresentation

take uiimageview image and convert to uiimage (swift4)


I have a image view with a photo in it. I want to take that photo and convert it it to a UIImagePNGRepresentation. In a effort to store it as core Data. What I have coded right now does no work.

    import UIKit

class editVCViewController: UIViewController {
    @IBOutlet var imageV: UIImageView!


    @IBAction func press(_ sender: Any) {


        var image = UIImage(named: self.imageV)
        var rep = UIImagePNGRepresentation(image!)

    }

}

Solution

  • You have

    var image = UIImage(named: self.imageV)
    

    That won't compile, because imageV is an image view, not the (string) name of an image in your bundle.

    Just say

    var image = self.imageV.image