iosobjective-ccore-graphicscgcontextdrawpath

CGContextDrawPath only strokes and does not fill


I am trying to make a shape that strokes and fills but it does not fill.

Here is my code:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGContextBeginPath(ctx);

    self.dottedLineColor = [UIColor whiteColor];
    self.solidLineColor = [UIColor blackColor];
    CGContextSetStrokeColorWithColor(ctx, [self.dottedLineColor CGColor]);
    CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);

    CGContextSetLineWidth(ctx, 11.0);

    for (NSArray *array in previewPoints){
            CGPoint pointMadeFromString1 = CGPointFromString([array objectAtIndex:0]);
            CGPoint pointMadeFromString2 = CGPointFromString([array objectAtIndex:1]);

            CGPathMoveToPoint(pathRef, NULL, pointMadeFromString1.x, pointMadeFromString1.y);
            CGPathAddLineToPoint(pathRef, NULL, pointMadeFromString2.x, pointMadeFromString2.y);
            /*
             CGContextMoveToPoint(ctx, pointMadeFromString1.x, pointMadeFromString1.y);
             CGContextAddLineToPoint(ctx, pointMadeFromString2.x, pointMadeFromString2.y);

             CGContextSetLineWidth(ctx, 11.0);
             const CGFloat dashPattern[2] = {2, 0};
             CGContextSetLineDash(ctx, 2, dashPattern, 2);
             CGContextStrokePath(ctx);
             */
    }
    CGContextAddPath(ctx, pathRef);
    CGContextClosePath(ctx);

    CGContextDrawPath(ctx, kCGPathFillStroke);
    CGContextFillPath(ctx);
}

Here is it how it looks now:

enter image description here

The area in the middle should be black.

How would I fix this?


Solution

  • The issue was that every time I was using CGPathMoveToPoint which would mean that it couldn't fill since it was not actually a closed path. I switched it so that only the first item in the array would use CGPathMoveToPoint and then all of the others would use CGPathAddLineToPoint.