0

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Anvil
  • 1,745
  • 1
  • 11
  • 16
  • 3
    `if let imageData = image.pngData()` OR `if let imageData = UIImagePNGRepresentation(image)`, don't do it twice. – Larme Jan 17 '19 at 17:24

1 Answers1

0

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() {   
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • FYI - If the `pngData` method is available then attempting to use `UIImagePNGRepresentation` will result in a deprecation warning. – rmaddy Jan 17 '19 at 17:39
  • @rmaddy `pngData()` was introduced in Swift 4.2 , and added that way because i recommend it more than the old way – Shehata Gamal Jan 17 '19 at 17:44
  • I get that. But it makes no sense to recommend using `UIImagePNGRepresentation` when `pngData` is available. We know `pngData` is available to this user because they are using it. – rmaddy Jan 17 '19 at 17:46
  • Swift 5 converting image to data is: JPEGIMAGE.jpegData(compressionQuality: 1) – Ahmadreza Jun 19 '19 at 10:09