2010-07-12 42 views
1

我正在为iPhone制作一个简单的绘图程序。目前,触摸事件将它们的位置添加到与一系列CGContextAddLineToPoint调用相关联的列表中,该列表在每次调用drawRect时都会重绘。缓存到CGLayer比iPhone上的重绘要慢

我遇到性能问题,但行数相当少(它们是透明的,但是),所以我尝试将所有内容都绘制到CGLayer中。现在,不是每一帧都绘制每一行,而是绘制每行一次,并在每一帧绘制CGLayer到屏幕。

 CGContextRef backgroundContext = CGLayerGetContext(pathBuffer); 
     CGContextSetLineWidth(backgroundContext, 2.0); 
     CGContextSetLineCap(backgroundContext, kCGLineCapButt); 
     CGContextSetStrokeColorWithColor(backgroundContext, [pathColor CGColor]); 

     if([[touchPoints objectAtIndex:0] continuesToNextPoint]) 
     { 
      CGContextMoveToPoint(backgroundContext, [[touchPoints objectAtIndex:0] point].x, [[touchPoints objectAtIndex:0] point].y); 
      CGContextAddLineToPoint(backgroundContext, [[touchPoints objectAtIndex:1] point].x, [[touchPoints objectAtIndex:1] point].y); 
      CGContextStrokePath(backgroundContext); 
     } 

     //remove it from touchpoints 
     [touchPoints removeObjectAtIndex:0]; 
    } 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextDrawLayerAtPoint(context, CGPointMake(0, 0), pathBuffer); 

这个,如果有的话,比我以前做的要慢。难道我做错了什么?我会更好地服务于绘制UIImage或什么?谢谢。

回答

1

你应该做的是在你的一个视图中插入一个CALayer,更新图层,然后调用setNeedsDisplayInRect:,并在图层上改变区域的边界,这会导致它更新。如果您尝试更新整个屏幕,速度会变慢。

您是否在使用仪器来确定哪些部件很慢?我建议用它来了解哪些部分会让你放慢速度。

+0

它看起来好像处理时间的30%是去CGContextStrokePath。 – jonmorgan 2010-07-12 19:40:56

+0

顺便提一下,感谢乐器的领导 - 如果你不能说,我对此很新。 – jonmorgan 2010-07-12 19:49:12

+0

谢谢,这解决了这个问题。 – jonmorgan 2010-07-14 15:35:25