Search code examples
swiftuiimagepngrepresentation

Cannot convert value of type 'Data?' to expected argument type 'UIImage


I'm trying to get the data of an image but I'm getting this error:

Cannot convert value of type 'Data?' to expected argument type 'UIImage'

The code:

if let image = profileImageView.image {
    if let imageData = UIImagePNGRepresentation(image.pngData()) {
        PFUser.current()?["photo"] = PFFileObject(name: "profile.png", data: imageData)
    }
}    

Where have I gone wrong?


Solution

  • The initializer of UIImagePNGRepresentation takes a UIImage instance not Data instance , so replace

    if let imageData = UIImagePNGRepresentation(image.pngData()) {
    

    with

    if let imageData = UIImagePNGRepresentation(image) {
    

    OR better use the latest Way

    if let imageData = image.pngData() {