Re: Drawing an NSImage in a CALayer
Re: Drawing an NSImage in a CALayer
- Subject: Re: Drawing an NSImage in a CALayer
- From: David Duncan <email@hidden>
- Date: Fri, 19 Sep 2008 13:27:44 -0700
On Sep 19, 2008, at 10:48 AM, Matt Long wrote:
p.s. I would defer to David Duncan on the CGImageRef creation
overhead. I'm sure he's right. I just didn't have the time to try to
get his code to work. ;-)
I forgot to set the current context :). More complete code here,
although this doesn't take the real resolution of the image into
account (-size returns a value in points, not pixels).
-(CGImageRef)nsImageToCGImageRef:(NSImage*)nsimage;
{
NSSize imageSize = [nsimage size];
CGColorSpaceRef genericRGB =
CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width,
imageSize.height, 8, 0, genericRGB, kCGImageAlphaPremultipliedFirst);
NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext
graphicsContextWithGraphicsPort:context flipped:NO];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:nsGraphicsContext];
[[NSColor yellowColor] setFill];
NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height));
[nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect
operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext setCurrentContext:nsGraphicsContext];
CGImageRef image = CGBitmapContextCreateImage(context);
CFRelease(context);
return image;
}
--
David Duncan
Apple DTS Animation and Printing
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden