2012-02-22 58 views
19

我需要绘制一个六边形,并用图像作为模式填充颜色。 我所做的:CoreGraphics FillPath和描边路径

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

但从CGContextStrokePath和CGContextFillPath的顺序根据,我得到一个六边形接壤,但没有填充或填充但不接壤。我怎样才能解决这个问题?

+0

增加CGContextSetLineWidth中的宽度.. – 2012-02-22 15:29:55

回答

31

尝试

CGContextDrawPath(context, kCGPathFillStroke); 

而不是

CGContextStrokePath(context); 
CGContextFillPath(context); 
+0

很好用。谢谢。 – IssamTP 2012-02-22 15:45:21

+0

你做了我的一天。谢谢 – 2017-05-25 17:01:21

0

要么使用

CGContextDrawPath(context, kCGPathFillStroke); 

为SCH建议,或致电FillPath前再次提请六边形。 StrokePath和FillPath删除已添加到上下文的路径,因此下一次调用将在没有路径的情况下以静默方式失败。

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

注意:这两段代码并不相同,并给出不同的结果。