• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Drawing an NSImage in a CALayer
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Drawing an NSImage in a CALayer (From: Brad Gibbs <email@hidden>)
 >Re: Drawing an NSImage in a CALayer (From: Matt Long <email@hidden>)
 >Re: Drawing an NSImage in a CALayer (From: Brad Gibbs <email@hidden>)
 >Re: Drawing an NSImage in a CALayer (From: Matt Long <email@hidden>)

  • Prev by Date: Re: async NSOperation and NSOperationQueue
  • Next by Date: Re: Differences between -isEqual: and -isEqualTo:?
  • Previous by thread: Re: Drawing an NSImage in a CALayer
  • Next by thread: Re: Drawing an NSImage in a CALayer
  • Index(es):
    • Date
    • Thread