如何将包含Core Animation图层的视图渲染为位图? [英] How do I render a view which contains Core Animation layers to a bitmap?

查看:160
本文介绍了如何将包含Core Animation图层的视图渲染为位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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

但是,当我使用这个代码时,Core

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< 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 呈现到位图?我不能在任何地方找到任何示例代码这样做,事实上,我找不到任何示例代码 CARenderer

Is there some other way to get the CALayers 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.

推荐答案

有一个伟大的文章可可是我的女朋友关于录制核心动画。作者将整个动画捕获到一部电影中,但您可以使用他抓取单个框架的部分。

跳到本文中的获取当前帧部分:

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's renderInContext:


  • 上下文中创建NSBitmapImageRep(使用
    CGBitmapContextCreateImage和
    NSBitmapImageRep的initWithCGImage)

  • Create a CGContext
  • Use CALayer's renderInContext:
  • Create a NSBitmapImageRep from the context (using CGBitmapContextCreateImage and NSBitmapImageRep's 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

这篇关于如何将包含Core Animation图层的视图渲染为位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆