Ios5 绘制多个矩形的最佳方法?

Ios5 绘制多个矩形的最佳方法?,ios5,quartz-graphics,Ios5,Quartz Graphics,在我的项目中,我需要用石英画几百个矩形。我确实有这样的密码 -(void)RenderRectangles:(NSArray*)rectangles fillColor:(UIColor*)fillColor strokeColor:(UIColor*)strokeColor strokeThickness:(float)strokeThickness; { CGConte

在我的项目中,我需要用石英画几百个矩形。我确实有这样的密码

-(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

创建单一路径更有效。检查以下函数以使用矩形创建单个路径,然后可以填充和绘制相同的路径,而无需重新创建路径:

CGPathCreateMutable()
CGPathAddRect()/CGPathAddRects()
CGContextAddPath()
…为了提高性能,如果要多次绘制该路径,请记住缓存该路径