我正在尝试使用Swift从OS X将图像文件上传到Parse.com。搜索Parse.com文档(对于OS X),我找到了以下代码:
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()
问题是它使用UIImagePNGRepresentation(),它来自iOS API,而不是OS X.
有没有人知道如何在OS X和Swift上正确地做到这一点?
答案 0 :(得分:0)
相当于UIImagePNGRepresentation
可能是这样的东西:
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()