用渐变绘制路径的笔划

时间:2010-12-09 12:19:52

标签: objective-c performance graphics

我正在尝试绘制Bezier曲线的笔划,线性渐变从红色变为绿色。我现在所做的是:

NSBezierPath* path = [NSBezierPath bezierPath];
[path setLineWidth: 1];

NSPoint startPoint = {  10, 100 };
NSPoint endPoint   = { 590, 500 };

int r1 = arc4random() % 1000;
int r2 = arc4random() % 1000;
NSPoint cp1 = { 700, -500 + r1 };
NSPoint cp2 = { -500 + r2, 700 };

[path  moveToPoint: startPoint];
[path curveToPoint: endPoint
     controlPoint1: cp1
     controlPoint2: cp2];

if (curves.count == 50) {
    [curves removeObjectAtIndex:0];
}
[curves addObject:path];

int i = 0;
for (NSBezierPath * p in curves) {
    [[redColors objectAtIndex:i++] set];
    [p stroke];
}

这很有效,但当我将NSBezierPath path转换为CGPathRef myPath = [path quartzPath]并迭代'CGPathRef'而不是'NSBezierPath'时:

CGPathRef myPath = [path quartzPath];
if (curves.count == size) {
    [paths removeObjectAtIndex:0];
}
[paths addObject:myPath];

CGContextRef c = [[NSGraphicsContext currentContext]graphicsPort];
for (int i = 0; i < paths.count; i++) {
    [self drawPath:c :[paths objectAtIndex:i]:i];
}

我的表现从大约30 FPS下降到5 FPS!

这是我的drawPath代码:

-(void) drawPath:(CGContextRef) c: (CGPathRef) myPath: (int) i {
    CGContextSaveGState(c);
    CGContextAddPath(c, myPath);
    CGContextReplacePathWithStrokedPath(c);
    CGContextClip(c);

    //  Draw a linear gradient from top to bottom
    CGContextDrawLinearGradient(c, cfGradients[i], start, end, 0);

    CGContextRestoreGState(c);
}

redColorscfGradients是存储alpha为0-1 / 0-255的元素的数组,因此不需要在每次迭代时重新创建它们。

这种性能甚至比Java差很多。当然,必须有一种方法可以更有效地绘制笔划,而不会从NSBezierPath转换为CGPathRef等。

请帮忙。

0 个答案:

没有答案