在我的应用程序中,我有一个根层,以及许多作为 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/