2

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?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Try with this and tell us if it worked: http://stackoverflow.com/questions/28229958/get-png-representation-of-nsimage-in-swift – Eric Aya Jan 19 '16 at 14:47

1 Answers1

0

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()
Sebastian
  • 6,154
  • 5
  • 33
  • 51
  • 1)what is image? 2)what is `PFFile` and `PFObject`? They don't exist in apple documentation so they are 3rd-party libraries. If you mention them then you should also specify where to find them – Gargo Sep 17 '17 at 08:15
  • 3
    It's the pngData line. I only insert the line in the code provide by Plinio. Did you read the question's code ? – Sebastian Sep 18 '17 at 01:45