22

I need to draw an hexagon and fill it with a color build with an Image as pattern. I did:

CGContextSaveGState(context);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetFillColorWithColor(context, [[UIColor colorWithPatternImage:[UIImage imageNamed:@"patternerba.png"]] CGColor]);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetLineWidth(context, 3.0);
// drawing hexagon here...
CGContextStrokePath(context);
CGContextFillPath(context);
[[NSString stringWithFormat:@"Foo"] drawAtPoint:innerRect.origin withFont:[UIFont fontWithName:@"Helvetica" size:16]];
CGContextRestoreGState(context);

But depending from the order of CGContextStrokePath and CGContextFillPath, I get an hexagon bordered but not filled or filled but not bordered. How can I fix this?

1
  • increase the width in CGContextSetLineWidth.. Commented Feb 22, 2012 at 15:29

2 Answers 2

37

Try

CGContextDrawPath(context, kCGPathFillStroke);

Instead of

CGContextStrokePath(context);
CGContextFillPath(context);
1
0

Either use

CGContextDrawPath(context, kCGPathFillStroke);

as sch suggested, or draw the hexagon again before calling FillPath. StrokePath and FillPath remove the path that you have added to the context, thus the next call will silently fail without a path.

CGPathRef path = /* drawing hexagon here */;
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGContextAddPath(context, path);
CGContextFillPath(context);

Note: the two segments of code are not equivalent and gives different result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.