views:

302

answers:

2

I'm currently using CGContextDrawLayerInRect to draw a CGLayer in drawRect. This is called quite often. Is there any way to lessen the overhead by drawing only that part of the CGLayer which has changed instead of the whole CGLayer in drawRect?

-(void)drawRect:(CGRect)rect {
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextDrawLayerInRect(currentContext, [self bounds], _backgroundLayer);
}
A: 

The rect parameter does describe the area which needs to be redrawn. Other than using this to limit how much you repaint, no. When drawrect is called, you have no idea what the system has done with your layer - for example, you could be prerendering the entire view into a new context for a system animation.

Are you actually seeing poor performance? Maybe your drawing code can be optimised in other ways.

Roger Nolan
A: 

This is pretty much as optimized as you can get, I think. Would it be possible to call setNeedsDisplay less often, maybe? If this is too slow for you, your next best bet is to use OpenGL!

Ben Gotow