Flatten CALayer sublayers into one layer

前端 未结 2 2036
予麋鹿
予麋鹿 2021-01-03 12:36

In my app I have one root layer, and many images which are sublayers of rootLayer. I\'d like to flatten all the sublayers of rootLayer into one layer/image, that don\'t have

相关标签:
2条回答
  • 2021-01-03 12:49

    From your own example for 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;
    

    Also note the caveat in the docs:

    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.

    0 讨论(0)
  • 2021-01-03 12:58

    Do you still want the layer to be interactive? If not, call -renderInContext: and show the bitmap context.

    0 讨论(0)
提交回复
热议问题