CGContext优化 [英] CGContext Optimization
问题描述
我的运行速度有一个小问题。在加载时,我生成一个CGMutablePath,它至少包含1000个点。我想在屏幕上滚动此路径,所以我使用这种代码:
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);
}
问题是它有点慢,不是很慢,但正如我需要添加更多的图形代码...
我想知道设备是否正在绘制整个路径(一旦应用了比例,该路径大约为10.000像素宽),或者只是可见的部分屏幕 ?我可以做些什么来优化它?
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 ?
推荐答案
尝试了很多事情之后(例如将我的1000点积分路径分成较小的10点点路径等...),我已经使用简单的顶点数组将所有内容移植到了openGL。
我花了大约4个小时来移植所有内容(甚至缩放路径,滚动等等),现在,我一直都得到完整的60fps动画,当时我的平均峰值只有22fps使用CoreGraphics时为28。
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.
这篇关于CGContext优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!