我如何呈现,其中包含核心动画层位图一个看法? [英] How do I render a view which contains Core Animation layers to a bitmap?
问题描述
我使用的是的NSView
来托管多个核心动画的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
然而,当我用这个code,核心动画层不会被渲染。
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 - 层LT; CALayer的:0x1092ea0>已经连接到上下文
我猜这一定是因为该层树在我的的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
s到渲染为位图?我找不到任何样品code这样做的任何地方,其实我找不到任何样品$ C $为 CARenderer
在所有的C
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.
推荐答案
有一个伟大的职位上可可是我的女朋友关于录制动画的核心。作者抓住了整个动画拍成电影,但你可以使用他抓起一帧的部分。结果
跳转到本文中的获取当前帧部分:结果
<一href=\"http://www.cimgf.com/2009/02/03/record-your-core-animation-animation/\">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上
- 使用<一个href=\"http://developer.apple.com/mac/library/DOCUMENTATION/GraphicsImaging/Reference/CALayer%5Fclass/Introduction/Introduction.html#//apple%5Fref/occ/instm/CALayer/renderInContext%3A\">CALayer's
renderInContext:
- 从创建NSBitmapImageRep
(当使用
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
- CAOpenGLLayer
- QTMovieLayer
这篇关于我如何呈现,其中包含核心动画层位图一个看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!