【问题标题】:What is the equivalent of iOS's UIImagePNGRepresentation() for OS X? [duplicate]OS X 的 iOS 的 UIImagePNGRepresentation() 等价物是什么? [复制]
【发布时间】:2016-04-25 01:49:36
【问题描述】:

我正在尝试使用 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 上正确执行吗?

【问题讨论】:

标签: swift macos cocoa uiimagepngrepresentation


【解决方案1】:

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)PFFilePFObject 是什么?它们不存在于苹果文档中,因此它们是第 3 方库。如果你提到它们,那么你还应该指定在哪里可以找到它们
  • 这是 pngData 行。我只在 Plinio 提供的代码中插入该行。你读过问题的代码吗?
猜你喜欢
  • 1970-01-01
  • 2012-09-06
  • 2014-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-26
  • 2015-06-13
  • 1970-01-01
相关资源
最近更新 更多