views:

267

answers:

2

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 any sublayers. I think I should do this by drawing all sublayers in core graphics context, but I don't know how to do that.

I hope you'll understand me, and sorry for my English.

A: 

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

KennyTM
as I said I don't know how to do this.I wrote: http://en.pastebin.ca/1744529but it doesn't work.
radex
+2  A: 

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);

iPhone OS:

UIGraphicsBeginImageContext(rootLayer.bounds.size);
[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.

Costique
Thank you! Although it's shame that's filters, and 3d transforms are not supported.
radex