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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to