CGContext line drawing: CGContextFillPath not working?

牧云@^-^@ 提交于 2019-11-30 11:46:06

I think you need to call CGContextClosePath before you can fill the path. I am not sure what you are trying to draw, but CGContextFillPath will fill the area inside the path and I only see single line here.

To solve this problem, use the following code:

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineWidth(context, 8.0);

CGMutablePathRef pathRef = CGPathCreateMutable();

/* do something with pathRef. For example:*/
CGPathMoveToPoint(pathRef, NULL, x, y);
CGPathAddLineToPoint(pathRef, NULL, x, y+100);
CGPathAddLineToPoint(pathRef, NULL, x+100, y+100);
CGPathAddLineToPoint(pathRef, NULL, x+100, y);
CGPathCloseSubpath(pathRef);

CGContextAddPath(context, pathRef);
CGContextFillPath(context);

CGContextAddPath(context, pathRef);
CGContextStrokePath(context);

CGPathRelease(pathRef);

I believe that the best way to stroke and fill your path is to add the following line of code after you've finished setting up your path:

    CGContextDrawPath(myContext, kCGPathFillStroke);
lupengshou

I had the same trouble. I found that you can not use

CGContextStrokePath( UIGraphicsGetCurrentContext() ) 
CGContextFillPath( UIGraphicsGetCurrentContext() ) 

consecutively: the latter will not work because after committing the stroke on the path, the path is removed from the context.

So, I used the same path twice; one for stroke,the other for fill.

// ... create a path of CGMutablePathRef ....

CGContextSaveGState( context );
//....fill the path .....
CGContextRestoreGState( context );

CGContextSaveGState ( context );
//....stroke the path .....
CGContextRestoreGState( context );

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