1

我有一个小的运行速度问题。在加载时,我生成了一个至少包含 1000 个点的 CGMutablePath。我想在屏幕上滚动这条路径,所以我使用这种代码:

-(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 像素宽),或者只是屏幕上可见的部分?我能做些什么来优化这个?

4

2 回答 2

1

如果可能的话,每秒绘制 60 次 1,000 点或 10,000 点路径确实会减慢您的应用程序的速度。如果路径是静态的,您真的应该只在 UIView 中绘制一次,以便将其缓存在 UIView 的图层中,并在图层周围设置动画。如果它需要动画,您可以查看新的 CAShapeLayer,它提供了通过将动画应用于其路径属性来为贝塞尔路径设置动画的能力。

但是,10,000 像素将比 iPhone 上的最大纹理尺寸 (2048 x 2048) 宽,因此在任何标准视图或图层中显示时都会出现问题,可能需要将其分解为更小的块或使用 CATiledLayer将其渲染到屏幕上。

于 2009-07-30T12:09:38.797 回答
1

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.

于 2009-08-04T19:42:07.013 回答