如何使用 CoreAnimation 组合模型将 UIView 层次结构渲染到 CGContext 中

发布于 2024-10-09 01:23:45 字数 833 浏览 0 评论 0原文

我正在尝试将 UIView 层次结构渲染到 CGContext 或最终渲染到 UIImage 中。一些子视图具有 3D 变换,renderInContext: 会忽略这些变换以及阴影和其他一些 CALayer 属性。

来自 CALayer.h:

/*
 * WARNING: currently this method does not implement the full
 * CoreAnimation composition model, use with caution. */

- (void)renderInContext:(CGContextRef)ctx;

在调用 UIGraphicsBeginImageContext 后,我尝试了几种变体。

[self drawRect:[self bounds]];
[self.layer.delegate drawLayer:self.layer inContext:UIGraphicsGetCurrentContext()];
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
[self.layer display];

最好的情况是视图层次结构无需 3D 变换即可渲染。最坏的情况是图像是空的。我希望最终在图像中得到有效的 Alpha 通道,因此屏幕捕获不太有效。

有没有办法在保留所有变换和图层属性的同时呈现 UIView 层次结构?

如果解决方案涉及递归迭代视图层次结构、分别将每个视图渲染为图像,以及在每个父视图的合成步骤中应用所有变换和属性,那么如何应用 3D 变换?

I am trying to render a UIView hierarchy into a CGContext or ultimately a UIImage. Some of the child views have 3D transforms, which renderInContext: ignores along with shadows and some other CALayer properties.

From CALayer.h:

/*
 * WARNING: currently this method does not implement the full
 * CoreAnimation composition model, use with caution. */

- (void)renderInContext:(CGContextRef)ctx;

I have tried several variations after a call to UIGraphicsBeginImageContext.

[self drawRect:[self bounds]];
[self.layer.delegate drawLayer:self.layer inContext:UIGraphicsGetCurrentContext()];
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
[self.layer display];

At best the view hierarchy renders without 3D transforms. At worst the image is empty. I would like to end up with a valid alpha channel in the image, so a screen capture does not quite work.

Is there a way to render a UIView hierarchy while preserving all transforms and layer properties?

If the solution involves recursively iterating the view hierarchy, rendering each view as an image separately, and applying all transforms and properties in a composition step for each parent view, how are 3D transforms applied?

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

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

发布评论

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

评论(1

锦欢 2024-10-16 01:23:45

抱歉,但是你没有办法做到这一点。

图层的当前动画状态是私有的,无法访问。
这可能是由于性能原因。启用对可动画属性的直接值的访问并将这些值置于与 Core Graphics 兼容的结构中会降低图形性能。

I am sorry, but there is no way how you could do it.

The current animation state of a layer is private and cannot be accessed.
This is probably due to performance reasons. Enabling the access to immediate values of animatable properties and having the values in structures compatible with Core Graphics would decrease graphics performance.

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