Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get PNG representation of NSImage in swift

Tags:

swift

Hey I'm having some trouble with getting the PNG representation of an NSImage object.

Here's what I'm doing:

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

coverImgView is an NSImageView object

However, I cannot even get it compiled somehow.

It says: Type '[NSObject: AnyObject]' does not conform to protocol 'NilLiteralConvertible' (the third line of the code I post above, argument "properties: nil")

I'm trying to do something similar to the function "PNGRepresentationOfImage" here https://gist.github.com/mtabini/1178403

Any ideas ?

Thanks a lot~

like image 863
Yoope Avatar asked Jan 30 '15 05:01

Yoope


1 Answers

The documentation says:

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

So it expects a dictionary, not a nil value. Supply an empty dict like this:

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

Only if an Optional is specified (that is it where [NSObject : AnyObject]?) you could pass a nil value.

like image 161
qwerty_so Avatar answered Jan 05 '23 01:01

qwerty_so