Plinio Vilela
Plinio Vilela

Reputation: 21

What is the equivalent of iOS's UIImagePNGRepresentation() for OS X?

I'm trying to upload an Image File to Parse.com from OS X using Swift. Searching the Parse.com documentation (for OS X) I found the following code:

let imageData = UIImagePNGRepresentation(image)
let imageFile = PFFile(name:"image.png", data:imageData)

var userPhoto = PFObject(className:"UserPhoto")
userPhoto["imageName"] = "My trip to Hawaii!"
userPhoto["imageFile"] = imageFile
userPhoto.saveInBackground()

The problem is that it uses UIImagePNGRepresentation(), which is from the iOS API and not OS X.

Does anyone know how to do it correctly on OS X and Swift?

Upvotes: 2

Views: 2517

Answers (1)

Sebastian
Sebastian

Reputation: 6384

The equivalent of UIImagePNGRepresentation could be Something like this:

let cgImgRef = image.CGImageForProposedRect(nil, context: nil, hints: nil)
let bmpImgRef = NSBitmapImageRep(CGImage: cgImgRef!)
let pngData = bmpImgRef.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:])

// your code bellow

let imageFile = PFFile(name:"image.png", pngData)

var userPhoto = PFObject(className:"UserPhoto")
userPhoto["imageName"] = "My trip to Hawaii!"
userPhoto["imageFile"] = imageFile
userPhoto.saveInBackground()

Upvotes: 0

Related Questions