2011-12-03 30 views
0

在我的项目中,我需要使用Quartz绘画来绘制几百个矩形。我确实有这样的代码最好的方法来绘制多个矩形?

-(void)RenderRectangles:(NSArray*)rectangles 
         fillColor:(UIColor*)fillColor 
        strokeColor:(UIColor*)strokeColor 
      strokeThickness:(float)strokeThickness; 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    UIGraphicsPushContext(context); 

    CGContextSetStrokeColorWithColor(context, [strokeColor CGColor]); 
    CGContextSetLineWidth(context, strokeThickness); 

    for (NSValue *vRect in rectangles) { 
    CGContextAddRect(context, [vRect CGRectValue]); 
    } 
    CGContextStrokePath(context); 

    CGContextSetFillColorWithColor(context, [fillColor CGColor]); 
    for (NSValue *vRect in rectangles) { 
    CGContextFillRect(context, [vRect CGRectValue]); 
    } 

    UIGraphicsPopContext(); 
} 

它工作正常,但我只是想知道是否有可能使用只有一个循环吗?还是有更好的方法来描边和填充矩形的集合?

Thx

回答

0

创建单个路径效率更高。看看下面的函数来创建与矩形的单一路径,那么你可以填充和笔触相同的路径,而不是需要重新创建:

CGPathCreateMutable() 
CGPathAddRect()/CGPathAddRects() 
CGContextAddPath() 

...和性能,记得缓存,如果这条道路你会多次绘制它!

+0

这是很好的知道。谢谢 – Eugen