How do I render a view which contains Core Animation layers to a bitmap?
If you want sample code for how to render a CALayer hierarchy to an NSImage (or UIImage for the iPhone), you can look at the Core Plot framework's CPLayer and its -imageOfLayer method. We actually created a rendering pathway that is independent of the normal -renderInContext: process used by CALayer, because the normal way does not preserve vector elements when generating PDF representations of layers. That's why you'll see the -recursivelyRenderInContext: method in this code.
However, this won't help you if you are trying to capture from any of the layer types mentioned by weichsel (QCCompositionLayer, CAOpenGLLayer, or QTMovieLayer).
There is a great post on "Cocoa is my girlfriend" about recording Core Animations. The author captures the whole animation into a movie, but you could use the part where he grabs a single frame.
Jump to the "Obtaining the Current Frame" section in this article:
http://www.cimgf.com/2009/02/03/record-your-core-animation-animation/
The basic idea is:
- Create a CGContext
- Use CALayer's
renderInContext:
- Create a NSBitmapImageRep from the context (using CGBitmapContextCreateImage and NSBitmapImageRep's initWithCGImage)
Update:
I just read, that the renderInContext:
method does not support all kind of layers in Mac OS X 10.5.
It does not work for the following layers classes:
- QCCompositionLayer
- CAOpenGLLayer
- QTMovieLayer