如何将包含核心动画层的视图渲染为位图? [英] How do I render a view which contains Core Animation layers to a bitmap?
问题描述
我正在使用 NSView
来托管多个 Core Animation CALayer
对象.我想要做的是抓取视图当前状态的快照作为位图图像.
I am using an NSView
to host several Core Animation CALayer
objects. What I want to be able to do is grab a snapshot of the view's current state as a bitmap image.
对于普通的 NSView
使用类似这样的东西,这相对简单:
This is relatively simple with a normal NSView
using something like this:
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
但是,当我使用此代码时,不会渲染核心动画层.
However, when I use this code, the Core Animation layers are not rendered.
我已经调查了 CARenderer
,因为它似乎可以满足我的需求,但是我无法让它渲染我现有的图层树.我尝试了以下方法:
I have investigated CARenderer
, as it appears to do what I need, however I cannot get it to render my existing layer tree. I tried the following:
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]];
但是,当我这样做时,我得到了一个例外:
However, when I do this I get an exception:
CAContextInvalidLayer -- layer
我猜这一定是因为层树托管在我的 NSView
中,因此附加到其上下文.我不明白如何从 NSView
中分离图层树以将其呈现为位图,在这种情况下创建重复的图层树并非易事.
I'm guessing that this must be because the layer tree is hosted in my NSView
and therefore attached to its context. I don't understand how I can detach the layer tree from the NSView
in order to render it to a bitmap, and it's non-trivial in this case to create a duplicate layer tree.
是否有其他方法可以让 CALayer
渲染到位图?我在任何地方都找不到任何用于执行此操作的示例代码,实际上我根本找不到 CARenderer
的任何示例代码.
Is there some other way to get the CALayer
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 CARenderer
at all.
推荐答案
关于录制 Core Animations 的Cocoa 是我的女朋友"有一篇很棒的帖子.作者将整个动画捕捉到电影中,但您可以使用他抓取单个帧的部分.
跳转到本文的获取当前帧"部分:
http://www.cimgf.com/2009/02/03/record-your-core-animation-animation/
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/
基本思想是:
- 创建一个 CGContext
- 使用CALayer 的
renderInContext:
- 从上下文(使用CGBitmapContextCreateImage 和NSBitmapImageRep 的 initWithCGImage)
更新:
我刚刚读到 renderInContext:
方法不支持 Mac OS X 10.5 中的所有类型的层.它不适用于以下图层类:
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
- CAOpenGL层
- QTMovieLayer
这篇关于如何将包含核心动画层的视图渲染为位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!