Drawing an NSImage in a CALayer
Drawing an NSImage in a CALayer
- Subject: Drawing an NSImage in a CALayer
- From: Brad Gibbs <email@hidden>
- Date: Thu, 18 Sep 2008 14:54:56 -0700
Hi,
I'm trying to draw an NSImage (a PNG) in a CALayer. The goal is to
create a method that allows me to pass an NSImage as an argument to
create a layer-hosting view. I have:
-(id)drawButton: (NSView *)button withImage:(NSImage *)anImage {
...
// image layer
imageLayer=[CALayer layer];
[imageLayer drawLayer:imageLayer inContext:ctx];
imageLayer.masksToBounds=YES;
[imageLayer addConstraint:[CAConstraint
constraintWithAttribute:kCAConstraintMaxY
relativeTo:@"superlayer"
attribute:kCAConstraintMaxY
offset:-(height/2)]];
[imageLayer addConstraint:[CAConstraint
constraintWithAttribute:kCAConstraintMidX
relativeTo:@"superlayer"
attribute:kCAConstraintMidX]];
[titleLayer addSublayer:imageLayer];
[titleLayer layoutIfNeeded];
...
And I found this snippet in the Core Animation Cookbook:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
NSGraphicsContext *nsGraphicsContext;
nsGraphicsContext = [NSGraphicsContext
graphicsContextWithGraphicsPort:ctx
flipped:NO];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:nsGraphicsContext];
// ...Draw content using NS APIs...
NSRect aRect=NSMakeRect(10.0,10.0,30.0,30.0);
NSBezierPath *thePath=[NSBezierPath bezierPathWithRect:aRect];
[[NSColor redColor] set];
[thePath fill];
[NSGraphicsContext restoreGraphicsState];
}
But I don't know how to get the current CGContextRef for the second
parameter.
Thanks in advance.
Brad
_______________________________________________
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