• 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: "Steven Degutis" <email@hidden>
  • Date: Thu, 9 Oct 2008 23:58:11 -0500

Hi, I just came across this thread, and I solved a similar problem earlier.
This is the code I ended up using:

NSImage *oldImage;
CALayer *layer;

CGImageRef newImage = [[[oldImage representations] objectAtIndex:0]
CGImage];
[layer setContents:(id)newImage];

It may be too late for this specific situation, but maybe someone else can
use it.

~ Steven Degutis
http://www.degutis.org/

On Thu, Sep 18, 2008 at 4:54 PM, Brad Gibbs <email@hidden> wrote:

> 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
>



--
Steven Degutis
_______________________________________________

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

  • Prev by Date: Re: [NSApp stop:]
  • Next by Date: How do I debug this?
  • Previous by thread: Extending URL Loading System - NSURLResponse
  • Next by thread: How do I debug this?
  • Index(es):
    • Date
    • Thread