views:

32

answers:

2

Hi all,

I have created a CALayer (I added several shapes to the layer, but ultimately, I have a single CALayer), and I am having a LOT of trouble adding it to a CGImage. I have found some references to [CALayer renderInContext:ctx], but I am not sure how to implement it.

Has anyone done this before?

Cheers, Brett

A: 

Create a bitmap graphics context and use renderInContext: to draw the layer into it. You now have the layer in an image.

Chuck
+2  A: 

Try this...

UIGraphicsBeginImageContext(layer.bounds.size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Jordan
not thread-safe prior to OS 4.0, blah blah, lalala...
tc.
Latest docs state that all `UIGraphics*Context` functions are main thread only (so they aren't thread safe on any OS version). `CGBitmapContextCreate` is the thread-safe CoreGraphics function that `UIGraphicsBeginImageContext` is built on top of.
rpetrich
Also from the docs: DiscussionYou should call this function only when a bitmap-based graphics context is the current graphics context. If the current context is nil or was not created by a call to UIGraphicsBeginImageContext, this function returns nil.
TomH