Re: Drawing an NSImage in a CALayer
Re: Drawing an NSImage in a CALayer
- Subject: Re: Drawing an NSImage in a CALayer
- From: Brad Gibbs <email@hidden>
- Date: Thu, 18 Sep 2008 16:00:39 -0700
On Sep 18, 2008, at 3:18 PM, Matt Long wrote:
You've got some fundamental issues here.
That doesn't surprise me...
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."?
I'm trying to create two classes that generate views that will be used
as buttons. One class generates a button view with a title, the other
generates a view with an image. I'm using CA to animate a CIBloom
filter that pulses once when the button is pressed. There will be
several of each of these buttons in my UI, the only difference between
them will be the titles or images on them. So, I'm trying to
encapsulate the button-making behavior into a single class that I can
pass a title or image to in order to create a new instance of a
button, which will be displayed and controlled from the appropriate
view controller or window controller. I have the title button maker
working, but I'm stumbling with the image button maker, as you've
noticed.
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