同时使用CGContextFillPath和CGContextStrokePath?iOS



我正在使用CGContextFillPath绘制Quartz 2D形状,如下所示:

    CGContextRef conPattern = CGBitmapContextCreate(NULL,
                                                    shp.size.width*2,
                                                    shp.size.height*2,
                                                    8,
                                                    0,
                                                    rgbColorSpace,
                                                    (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
    CGContextSetLineWidth(conPattern, 10);
    CGContextSetStrokeColorWithColor(conPattern, [UIColor blackColor].CGColor);
    Line * start = [verticesPassed objectAtIndex:0];
    StandPoint * startPoint = start.origin;
    CGContextMoveToPoint(conPattern, ([startPoint.x floatValue]-shp.origin.x)*2 , ([startPoint.y floatValue]-shp.origin.y)*2);
    for (Line * vertice in verticesPassed) {
        StandPoint * standPoint = vertice.origin;
        CGContextAddLineToPoint(conPattern, ([standPoint.x floatValue]-shp.origin.x)*2, ([standPoint.y floatValue]-shp.origin.y)*2);
    }
    CGContextSetFillColorWithColor(conPattern, [UIColor yellowColor].CGColor);
    CGContextFillPath(conPattern);
    [self drawText:conPattern startX:0 startY:20 withText:standName];
    [self drawText:conPattern startX:0 startY:50 withText:standNumber];
    //Make the main image and color it with pattern.
    CGImageRef cgImage = CGBitmapContextCreateImage(conPattern);
    UIImage *imgPattern = [[UIImage alloc]initWithCGImage:cgImage];

这个效果很好,画出了我的填充形状。然而,我也想做的是展示已经画好的线条。

我尝试在CGContextStrokePath中添加,但没有成功:

Line * start = [verticesPassed objectAtIndex:0];
    StandPoint * startPoint = start.origin;
    CGContextMoveToPoint(conPattern, ([startPoint.x floatValue]-shp.origin.x)*2 , ([startPoint.y floatValue]-shp.origin.y)*2);
    for (Line * vertice in verticesPassed) {
        StandPoint * standPoint = vertice.origin;
        CGContextAddLineToPoint(conPattern, ([standPoint.x floatValue]-shp.origin.x)*2, ([standPoint.y floatValue]-shp.origin.y)*2);
    }
    CGContextSetFillColorWithColor(conPattern, [UIColor yellowColor].CGColor);
    CGContextFillPath(conPattern);
    CGContextStrokePath(conPattern);

有人能建议我如何做到这一点吗?

填充路径时会消耗路径。如果你想同时填充和笔划,那么你应该使用

CGContextDrawPath(conPattern, kCGPathFillStroke); // both fill and stroke

相关内容

  • 没有找到相关文章

最新更新