• 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
Drawing an NSImage in a CALayer
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Drawing an NSImage in a CALayer
      • From: Matt Long <email@hidden>
    • Re: Drawing an NSImage in a CALayer
      • From: David Duncan <email@hidden>
  • Prev by Date: Re: alloc]init] VS new]
  • Next by Date: Differences between -isEqual: and -isEqualTo:?
  • Previous by thread: Re: alloc]init] VS new]
  • Next by thread: Re: Drawing an NSImage in a CALayer
  • Index(es):
    • Date
    • Thread