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

5.3k Views Asked by At

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?

1

There are 1 best solutions below

4
Shehata Gamal On BEST ANSWER

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() {