Re: Drawing an NSImage in a CALayer
Re: Drawing an NSImage in a CALayer
- Subject: Re: Drawing an NSImage in a CALayer
- From: Matt Long <email@hidden>
- Date: Thu, 18 Sep 2008 16:18:07 -0600
You've got some fundamental issues here. This call in particular:
imageLayer drawLayer:imageLayer inContext:ctx];
It doesn't make sense.
-drawLayer:inContext is a delegate method. You are overriding drawing
functionality for the layer in question. Instead you would set the
layer's delegate to your app delegate (or whatever controller you're
using) and then implement drawLayer:inContext in your controller.
e.g.
[imageLayer setDelegate:self]
then implement
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
if( layer == imageLayer )
{
[layer setContents:imageRef];
}
}
There are other deeper problems here, so maybe I'll just answer how to
get a CGImageRef...
Use this code:
- (CGImageRef)nsImageToCGImageRef:(NSImage*)image;
{
NSData * imageData = [image TIFFRepresentation];
CGImageRef imageRef;
if(imageData)
{
CGImageSourceRef imageSource =
CGImageSourceCreateWithData(
(CFDataRef)imageData, NULL);
imageRef = CGImageSourceCreateImageAtIndex(
imageSource, 0, NULL);
}
return imageRef;
}
you then call [imageLayer setContents:imageRef] where imageRef is the
CGImageRef object returned.
It looks like you're making things more difficult than they need to
be. Maybe clarify a little what you are doing. What does this mean:
"The goal is to create a method that allows me to pass an NSImage as
an argument to create a layer-hosting view."?
Best regards,
-Matt
On Sep 18, 2008, at 3:54 PM, Brad Gibbs 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];
_______________________________________________
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