nyko
nyko

Reputation: 36

Trying to convert UIImage to PFFileObject in Swift 5.1

I'm using Parse (hosted by back4app) as backend for an iOS project. My problem is that I can't figure out how to convert an UIImage to PFFileObject.

I've tried

let image: UIImage = getSelectedUIImage()
let parseUser.profileImage = image

and

let image: UIImage = getSelectedUIImage()
let parseUser.profileImage = image.pngData()

The error shown by Xcode is "Cannot assign value of type 'UIImage?' to type 'PFFileObject?'" or "Cannot assign value of type 'Data?' to type 'PFFileObject?'". I think that it's necessary to convert that UIImage to Binary, but I don't know how and google isn't a big help at that point...

Any hint or advice is appreciated!

Stay healthy!

Upvotes: 0

Views: 277

Answers (1)

nyko
nyko

Reputation: 36

Thanks to Charles hint to "RTFM" I could solve this "problem" with following Code:

let image: UIImage = getSelectedUIImage()
let imageData = image.pngData()
let pfFile = PFFileObject(name:"avatar.png", data: imageData)
parseUser.avatar = pfFile
do {
    // Better use saveInBackground() here
    try parseUser.avatar.save()
}
catch {}
parseUser.save()

Upvotes: 1

Related Questions