CoreGraphics FillPath And Stroke Path

泄露秘密 提交于 2019-12-20 17:29:01

问题


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:


Try

CGContextDrawPath(context, kCGPathFillStroke);

Instead of

CGContextStrokePath(context);
CGContextFillPath(context);



回答2:


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.



来源:https://stackoverflow.com/questions/9397072/coregraphics-fillpath-and-stroke-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!