Warum ist UIBezierPath schneller als Core-Graphics-Pfad?

Ich war Herumspielen mit dem zeichnen von Pfaden, und ich bemerkte, dass in mindestens einigen Fällen, UIBezierPath übertrifft, was ich dachte wäre eine Core-Grafik entspricht. Die -drawRect: folgenden Methode erstellt zwei Pfade: einer UIBezierPath, und eine CGPath. Die Pfade sind identisch, außer für Ihre Standorte, sondern streichelte die CGPath dauert etwa doppelt so lange wie das streicheln der UIBezierPath.

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //Create the two paths, cgpath and uipath.
    CGMutablePathRef cgpath = CGPathCreateMutable();
    CGPathMoveToPoint(cgpath, NULL, 0, 100);

    UIBezierPath *uipath = [[UIBezierPath alloc] init];
    [uipath moveToPoint:CGPointMake(0, 200)];

    //Add 200 curve segments to each path.
    int iterations = 200;
    CGFloat cgBaseline = 100;
    CGFloat uiBaseline = 200;
    CGFloat xincrement = self.bounds.size.width / iterations;
    for (CGFloat x1 = 0, x2 = xincrement;
         x2 < self.bounds.size.width;
         x1 = x2, x2 += xincrement)
    {
        CGPathAddCurveToPoint(cgpath, NULL, x1, cgBaseline-50, x2, cgBaseline+50, x2, cgBaseline);
        [uipath addCurveToPoint:CGPointMake(x2, uiBaseline)
                  controlPoint1:CGPointMake(x1, uiBaseline-50)
                  controlPoint2:CGPointMake(x2, uiBaseline+50)];
    }
    [[UIColor blackColor] setStroke];
    CGContextAddPath(ctx, cgpath);

    //Stroke each path.
    [self strokeContext:ctx];
    [self strokeUIBezierPath:uipath];

    [uipath release];
    CGPathRelease(cgpath);
}

- (void)strokeContext:(CGContextRef)context
{
    CGContextStrokePath(context);
}

- (void)strokeUIBezierPath:(UIBezierPath*)path
{
    [path stroke];
}

Beide Pfade verwenden CGContextStrokePath(), so habe ich verschiedene Methoden zu streicheln, jedem Pfad, so dass ich sehen kann die Zeit, die von jedem Pfad in Instrumente. Unten sind typische Ergebnisse (call tree invertiert); Sie können sehen, dass -strokeContext: braucht 9.5 Sek., während -strokeUIBezierPath: dauert nur 5 Sek.:

Running (Self)      Symbol Name
14638.0ms   88.2%               CGContextStrokePath
9587.0ms   57.8%                 -[QuartzTestView strokeContext:]
5051.0ms   30.4%                 -[UIBezierPath stroke]
5051.0ms   30.4%                  -[QuartzTestView strokeUIBezierPath:]

Sieht es aus wie UIBezierPath ist irgendwie Optimierung der Pfad, der es schafft, oder bin ich das erstellen der CGPath in einer naiven Art und Weise. Was kann ich tun, um speed up my CGPath Zeichnung?

InformationsquelleAutor der Frage Caleb | 2011-06-13

Schreibe einen Kommentar