Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw NSBitmapImageRep in NSImageView

Tags:

xcode

image

cocoa

How can I draw a NSBitmapImageRep in a NSImageView?

like image 783
Thizzer Avatar asked Feb 11 '11 09:02

Thizzer


2 Answers

NSImage *im = [[[NSImage alloc] init] autorelease];
[im addRepresentation:bitmapRep];
[imageView setImage:im];
like image 163
Ken Avatar answered Sep 21 '22 21:09

Ken


Pretty easy:

NSImage *image = [[NSImage alloc] initWithCGImage:[bitmapRep CGImage] size:NSMakeSize(width,height)];

Then:

[imageView setImage:image];

Another possibility is by turning it into a TIFF ( - (NSData *)TIFFRepresentation ) and using that to create the NSImage object, but that could create quite some overhead.

Also keep in mind that the method of NSBitmapImageRep - (CGImageRef)CGImage is 10.5 or higher.

Edit:

For a cleaner solution look at @Ken's answer.

like image 45
Antwan van Houdt Avatar answered Sep 22 '22 21:09

Antwan van Houdt