Swift iOS'的等价物是什么;OS X的UIImagePNGRepresentation()?
我正在尝试使用Swift从OS X向Parse.com上传一个图像文件。搜索Parse.com文档(针对OS X)时,我发现以下代码:Swift iOS'的等价物是什么;OS X的UIImagePNGRepresentation()?,swift,macos,cocoa,uiimagepngrepresentation,Swift,Macos,Cocoa,Uiimagepngrepresentation,我正在尝试使用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
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上正确执行此操作吗?与
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()
试试这个,告诉我们它是否有效:1)什么是图像?2) 什么是
PFFile
和PFObject
?它们不存在于apple文档中,所以它们是第三方库。如果你提到它们,那么你还应该指定在哪里找到它们,这是pngData行。我只在Plinio提供的代码中插入该行。你读过问题代码了吗?