CGContext Optimization

主宰稳场 提交于 2019-12-08 06:31:54

问题


I have a small run speed problem. On load, I generate a CGMutablePath containing at least 1000 points. I want to scroll this path on the screen, so I use this kind of code :

-(void) drawRect:(CGRect)rect {

    /*
    Here, I have a timer calling drawRect 60 times per second.
    There's also code for the scale and currentTime, based on
    an MP3 playback (AVAudioPlayer);
    */

    CGContextRef ref = UIGraphicsGetCurrentContext();   
    CGContextClearRect(ref, [self frame]);

    CGContextSaveGState(ref);
    CGContextTranslateCTM(ref, s.width/2+currentTime, 1);
    CGContextScaleCTM(ref, scale, 1);
    CGContextAddPath(ref, myGraphPath);
    CGContextSetRGBFillColor(ref, .1, .1, .1, .8);
    CGContextFillPath(ref);
    CGContextRestoreGState(ref);
}

The problem is that it's a bit slow, not very much, but as I need to add a whole lot more of graphic code... I was wondering if the device is drawing the whole path (once the scale is applied, the path is about 10.000 pixels wide), or just the part visible on the screen ? What can I do to optimize this ?


回答1:


Drawing a 1,000-point or 10,000-point path 60 times per second is really going to slow your application down, if it's possible at all. If the path is static, you really should look at only drawing it once within your UIView, so that it is cached within the UIView's layer, and animating the layer around. It it needs to be animated, you could look at the new CAShapeLayer, which provides the ability to animate a Bezier path simply by applying an animation to its path property.

However, 10,000 pixels will be wider than the maximum texture size on the iPhone (2048 x 2048), so you'll have problems displaying that in any standard view or layer and may need to either break it up into smaller chunks or use a CATiledLayer to render it to the screen.




回答2:


After trying a lot of things (like dividing my huge 1000 points path into smaller 10 points paths etc...), I've ported everything to openGL, using simple vertex arrays. It took me about 4 hours to port everything (even scaling the path, scrolling it, etc...) and now, I get a full 60fps animation all the time, when I was having only 22fps average with peaks at 28 when using the CoreGraphics.



来源:https://stackoverflow.com/questions/1205345/cgcontext-optimization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!