Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I render a view which contains Core Animation layers to a bitmap?
    primarykey
    data
    text
    <p>I am using an <code>NSView</code> to host several Core Animation <code>CALayer</code> objects. What I want to be able to do is grab a snapshot of the view's current state as a bitmap image.</p> <p>This is relatively simple with a normal <code>NSView</code> using something like this:</p> <pre><code>void ClearBitmapImageRep(NSBitmapImageRep* bitmap) { unsigned char* bitmapData = [bitmap bitmapData]; if (bitmapData != NULL) bzero(bitmapData, [bitmap bytesPerRow] * [bitmap pixelsHigh]); } @implementation NSView (Additions) - (NSBitmapImageRep*)bitmapImageRepInRect:(NSRect)rect { NSBitmapImageRep* imageRep = [self bitmapImageRepForCachingDisplayInRect:rect]; ClearBitmapImageRep(imageRep); [self cacheDisplayInRect:rect toBitmapImageRep:imageRep]; return imageRep; } @end </code></pre> <p>However, when I use this code, the Core Animation layers are not rendered.</p> <p>I have investigated <code>CARenderer</code>, as it appears to do what I need, however I cannot get it to render my existing layer tree. I tried the following:</p> <pre><code>NSOpenGLPixelFormatAttribute att[] = { NSOpenGLPFAWindow, NSOpenGLPFADoubleBuffer, NSOpenGLPFAColorSize, 24, NSOpenGLPFAAlphaSize, 8, NSOpenGLPFADepthSize, 24, NSOpenGLPFANoRecovery, NSOpenGLPFAAccelerated, 0 }; NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:att]; NSOpenGLView* openGLView = [[NSOpenGLView alloc] initWithFrame:[self frame] pixelFormat:pixelFormat]; NSOpenGLContext* oglctx = [openGLView openGLContext]; CARenderer* renderer = [CARenderer rendererWithCGLContext:[oglctx CGLContextObj] options:nil]; renderer.layer = myContentLayer; [renderer render]; NSBitmapImageRep* bitmap = [oglView bitmapImageRepInRect:[oglView bounds]]; </code></pre> <p>However, when I do this I get an exception:</p> <p><code>CAContextInvalidLayer -- layer &lt;CALayer: 0x1092ea0&gt; is already attached to a context</code></p> <p>I'm guessing that this must be because the layer tree is hosted in my <code>NSView</code> and therefore attached to its context. I don't understand how I can detach the layer tree from the <code>NSView</code> in order to render it to a bitmap, and it's non-trivial in this case to create a duplicate layer tree.</p> <p>Is there some other way to get the <code>CALayer</code>s to render to a bitmap? I can't find any sample code anywhere for doing this, in fact I can't find any sample code for <code>CARenderer</code> at all.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload