Using CGContextFillPath and CGContextStrokePath Together ? iOS

1.4k views Asked by At

I am drawing a Quartz 2D shape using CGContextFillPath as follows :

    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];

This works well and draws my filled shape. What I want to do as well however is show the lines that have been drawn.

I tried added in CGContextStrokePath but with no luck :

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);

Can anyone suggest how I can achieve this ?

1

There are 1 answers

0
David Rönnqvist On BEST ANSWER

The path is consumed when you fill it. If you want to both fill and stroke then you should use

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