简体   繁体   中英

optimising CGLayer drawRect method

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

Perhaps try eliminating -drawRect: entirely. Draw into a UIImageView or a CALayer and let it handle it.

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM