【问题标题】:get PNG representation of NSImage in swift快速获取 NSImage 的 PNG 表示
【发布时间】:2015-03-29 13:53:14
【问题描述】:

嘿,我在获取 NSImage 对象的 PNG 表示时遇到了一些问题。

这就是我正在做的事情:

var imgData: NSData! = coverImgView.image!.TIFFRepresentation!
var bitmap: NSBitmapImageRep! = NSBitmapImageRep(data: imgData!)
var pngCoverImage = bitmap!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: nil)

coverImgView 是一个 NSImageView 对象

但是,我什至无法编译它。

它说:类型'[NSObject:AnyObject]'不符合协议'NilLiteralConvertible'(我在上面发布的代码的第三行,参数“properties:nil”)

我正在尝试在 https://gist.github.com/mtabini/1178403 处执行类似于“PNGRepresentationOfImage”函数的操作

有什么想法吗?

非常感谢~

【问题讨论】:

    标签: swift


    【解决方案1】:

    文档说:

    func representationUsingType(_ storageType: NSBitmapImageFileType,
                  properties properties: [NSObject : AnyObject]) -> NSData?
    

    所以它需要一个字典,而不是一个 nil 值。像这样提供一个空字典:

    var pngCoverImage = bitmap!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:])
    

    仅当指定了 Optional 时(即 [NSObject : AnyObject]?) 您可以传递 nil 值。

    【讨论】:

    • 是的,我的错 :-D。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 2018-06-29
    • 2011-02-07
    • 1970-01-01
    相关资源
    最近更新 更多