For a custom UIView I've implemented, I want to display some graphics in multiple CALayers that are added as subviews of my UIViews layer.
I cannot use my UIView as the delegate for the sublayers. A UIView may only be the delegate for one layer and that is the layer the UIView already has from the get go. Since I also didn't want to subclass CALayer or create a dedicated delegate, I thought that doing my rendering to an image and setting that as the sublayer's content would be a good idea.
Initial experiments show that I can make the whole thing work by going through something like this:
-(void) initCustomLayer: {
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext ();
// ... do the drawing in ctx ...
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
customLayer = [[CALayer alloc] init];
customLayer.bounds.size = self.bounds.size;
customLayer.contents = image.CGImage;
[self.layer addSublayer customLayer];
[customLayer release];
}
In the documentation it says that using a CGLayer for offscreen rendering is the preferred way of doing offscreen rendering. So I would like to do the drawing in a CGLayer.
What I can't seem to find out is whether there is a way of setting the CALayer's content from the CGLayer?
Is this possible and does it even make sense? Should I just stop worrying and go with the UIImage/CGImageRef based approach?