如何同时使用CGContextFillPath和CGContextStrokePath?iOS

3
我正在使用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);

有人可以建议我怎样达到这个目标吗?
1个回答

10

当你填充路径时,路径会被消耗。如果你想要同时进行填充和描边,则应该使用

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接