Get NSImage PNGs in Fast

Hi, I am having problems getting a PNG representation of an NSImage object.

That'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 - NSImageView object

However, I can’t even compile it.

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

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

Any ideas?

Thanks a lot ~

+6
source share
1 answer

The documentation says:

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

Therefore, it expects a dictionary, not a nil value. Put an empty file like this:

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

Only if the Optional parameter is specified (that is, where [NSObject : AnyObject]?) , Can you pass the value nil.

+9
source

Source: https://habr.com/ru/post/981746/


All Articles