代码之家  ›  专栏  ›  技术社区  ›  radex

将CALayer子层展平为一层

  •  2
  • radex  · 技术社区  · 15 年前

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

    2 回复  |  直到 15 年前
        1
  •  9
  •   leoh    9 年前

    根据您自己的Mac OS X示例:

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

    网间网操作系统:

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

    还要注意文档中的注意事项:

    的Mac OS X v10.5实现 此方法不支持 全核心动画合成 模型QCCompositionLayer, CAOPENGLAYER和QTMovieLayer层 没有渲染。此外,层 使用三维变换的方法不适用 背景过滤器,过滤器,

        2
  •  1
  •   kennytm    15 年前

    您仍然希望层是交互式的吗?如果没有,请致电 -renderInContext: 并显示位图上下文。