Chris Gerken
Chris Gerken

Reputation: 16392

Transparency and CGLayers

I'm creating a number of CGLayer's for my iOS5 app like this:

layer_1 = CGLayerCreateWithContext (context,self.bounds.size, NULL);    
offlineContext_1 = CGLayerGetContext (layer_1);
layer_2 = CGLayerCreateWithContext (context,self.bounds.size, NULL);    
offlineContext_2 = CGLayerGetContext (layer_2);
layer_3 = CGLayerCreateWithContext (context,self.bounds.size, NULL);    
offlineContext_3 = CGLayerGetContext (layer_3);

For each step of the timer I draw to the offline_contexts and when all the drawing is complete I then collect all of the layers into one by calling:

CGContextDrawLayerAtPoint (context, CGPointZero, layer_1);
CGContextDrawLayerAtPoint (context, CGPointZero, layer_2);
CGContextDrawLayerAtPoint (context, CGPointZero, layer_3);

This works great. The content of layer 1 appears overdrawn by the content of layer 2 and then layer 3 overwrites the first two layers. All of the drawing is done using calls like:

    CGContextAddArc(context,cx,cy,radius,-fromDir,toDir,1-curve);

For performance reasons I replaced a lot of the repetitive drawing commands like the above with images so that the results should be the same, but instead of drawing CGContextAddArc() 20 times I tell the image to draw itself 20 times:

[myArcImage drawAtPoint: loc];

With that one change, however, the order of overlay seems to have changed. The images do get drawn to the collective view eventually, but only those portions of the images that do not overlay other lines and arcs drawn to the other contexts. I've played with using blending in the drawAtPoint variant, but that only affects the portions of the image that are visible.

Any ideas on why lines and arcs overlay content in other layers but images do not?

many thanks

Upvotes: 2

Views: 204

Answers (1)

Chris Gerken
Chris Gerken

Reputation: 16392

OK, it dawned on me what I was doing. The arcs and lines I was drawing were similar to the CGContextAddArc() call above and that call takes as a parameter the context on which to draw. When I switched over to drawing images, the drawAtPoint() method does not take a context as an argument. Instead it draws the image to the current context, which in my case was the context for the current UIView.

The solution to my problem is to wrap the drawAtPoint call with a push/pop context call:

UIGraphicsPushContext(correct_context);
[myArcImage drawAtPoint: loc];
UIGraphicsPopContext();

Upvotes: 1

Related Questions