CAShaperLayer -renderInContext 不起作用?

发布于 2024-08-02 09:37:40 字数 602 浏览 4 评论 0原文

我可以使用以下代码从核心动画层创建 UIImage:

- (UIImage*)contentsImage;
{
   UIGraphicsBeginImageContext([self bounds].size);
   [self renderInContext:UIGraphicsGetCurrentContext()];
   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return image;
}

此代码位于我的 CALayer 派生类中。我遇到的问题是我有两个 CAShapeLayers,它们是我的图层的子图层,不会渲染到结果图像。如果我将标准 CALayers 添加为子项,它们会渲染得很好。苹果文档说:

渲染接收器及其子层 进入指定的上下文。

它还说它从 iPhone OS 2.0 开始就可用。想知道我是否遗漏了什么,或者我是否应该提交雷达报告。

有什么想法可以阻止子 CAShapeLayers 被图像吸引吗?

谢谢。

I am able to create a UIImage from a Core Animation layer using the following code:

- (UIImage*)contentsImage;
{
   UIGraphicsBeginImageContext([self bounds].size);
   [self renderInContext:UIGraphicsGetCurrentContext()];
   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return image;
}

This code is in my CALayer derived class. The issue I am running into is that I have two CAShapeLayers that are child layers of my layer that do not get rendered to the resulting image. If I add standard CALayers as children they get rendered fine. The Apple docs say:

Renders the receiver and its sublayers
into the specified context.

It also says that it's been available since iPhone OS 2.0. Wondering if there is something I'm missing or if I should file a radar.

Any ideas what might keep the child CAShapeLayers from getting drawn to the image?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌面超妹 2024-08-09 09:37:40

CALayer 机制调用 renderInContext 来创建其位图内容属性。但在 CAShapeLayer 中,路径属性实际上并未呈现为其内容,如标题中的注释所示:

形状作为一个整体被合成
层的内容与其之间
第一个子层。

按理说, renderInContext 实际上不会将 CAShapeLayer 路径渲染到您的上下文中。不过,我自己实际上还没有尝试过这一点。

The CALayer machinery calls renderInContext to create its bitmapped contents property. But in a CAShapeLayer, the path property is not actually rendered to its contents as seen by this note in the header:

The shape as a whole is composited
between the layer's contents and its
first sublayer.

It stands to reason that renderInContext won't actually render the CAShapeLayer path onto your context. I haven't actually tried this out for myself however.

作死小能手 2024-08-09 09:37:40

不知道它是否与您相关,但 CALayer 文档中关于 renderInContext 的注释说:

**Important**: The Mac OS X v10.5 implementation of this method does not
support the entire Core Animation composition model. QCCompositionLayer, 
CAOpenGLLayer, and QTMovieLayer layers are not rendered. Additionally,
layers that use 3D transforms are not rendered, nor are layers that specify 
backgroundFilters, filters, compositingFilter, or a mask values. 
Future versions of Mac OS X may add support for rendering these layers
and properties.

无论如何,当我在图像上下文中结合使用 UIView drawRect 函数和绘图时遇到了类似的问题。如果我调用drawRect,包含子视图的整个UIView将不会绘制其子视图(这实际上是有意义的,因为它在文档中说,如果您调用drawRect,您将负责填充整个区域,无论超级视图和子视图实现如何)。我通过在所有子视图上调用drawRect,将它们传递给它们自己的框架来解决我的问题。

所以我建议也许放弃 renderInContext 并使用 CALayer 的 drawInContext 代替?您需要重写该方法,因为它默认情况下不执行任何操作。您的子类还需要将上下文移动到适当的框架。另外为了安全起见,您可能需要检查您添加的代码是否不会影响这些层的正常渲染。

Don't know if its relevant to you but there is a note in the CALayer documentation for renderInContext that says :

**Important**: The Mac OS X v10.5 implementation of this method does not
support the entire Core Animation composition model. QCCompositionLayer, 
CAOpenGLLayer, and QTMovieLayer layers are not rendered. Additionally,
layers that use 3D transforms are not rendered, nor are layers that specify 
backgroundFilters, filters, compositingFilter, or a mask values. 
Future versions of Mac OS X may add support for rendering these layers
and properties.

Anyways, I ran into a similar problem when using the UIView drawRect function in conjunction with drawing in an image context. The overall UIView that contained subviews would not draw its subviews if I called drawRect (which makes sense now actually since it says in the documentation if you call drawRect you are responsible for filling that entire area regardless of super and subview implementations). I solved my problem by just called drawRect on all my subviews, passing them their own frames.

So I would suggest maybe switching away from renderInContext and use CALayer's drawInContext instead? You'll need to override the method since it doesn't do anything by default. Your subclasses will also need to move the contexts to their appropriate frames. Also to be safe you might want to check that none of the code you add affects normal rendering of these layers.

活泼老夫 2024-08-09 09:37:40

我对此提出了雷达。我在文档中看不到任何它不应该工作的原因。如果/当苹果回复雷达时,我会在这里回复。

I filed a radar on this. I can't see any reason in the docs that it shouldn't work.I will respond back here if/when Apple replies to the radar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文