objective-c - 将 CALayer 子层压平为一层

标签 objective-c cocoa core-animation core-graphics

在我的应用程序中,我有一个根层,以及许多作为 rootLayer 的子层的图像。我想将 rootLayer 的所有子层展平为一个没有任何子层的层/图像。我认为我应该通过在核心图形上下文中绘制所有子图层来做到这一点,但我不知道该怎么做。

我希望你能理解我,很抱歉我的英语。

最佳答案

来自您自己的 Mac OS X 示例:

CGContextRef myBitmapContext = MyCreateBitmapContext(800,600);
[rootLayer renderInContext:myBitmapContext];
CGImageRef myImage = CGBitmapContextCreateImage(myBitmapContext);
rootLayer.contents = (id) myImage;
rootLayer.sublayers = nil;
CGImageRelease(myImage);

iOS:

UIGraphicsBeginImageContextWithOptions(rootLayer.bounds.size, NO, 0.0);
[rootLayer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
rootLayer.contents = (id) layerImage.CGImage;
rootLayer.sublayers = nil;

另请注意文档中的警告:

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.

关于objective-c - 将 CALayer 子层压平为一层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2034674/

相关文章:

ios - 怎么打重法?

ios - 如何从 ios 中的 Xib 文件加载自定义 UITableViewCells

macos - Webview 确实完成了 HTML5 视频播放器未调用的框架的加载

objective-c - NSEvent addGlobalMonitorForEventsMatchingMask 不适用于 CLI 应用程序

ios - 为什么我不能让我的动画持续时间为 0 秒?我将如何正确地做到这一点?

objective-c - 如何在设置动画时更改 UIView block 动画的速度(或持续时间)?

ios - UILabel AutoShrink 属性在 iOS 7 中不起作用

ios - 编译 ReactiveCocoa 时出错

objective-c - 我必须在每一行上转换吗?

ios - 翻转容器内的 View ,显示第二个 View