CGContext线条图:CGContextFillPath不工作?

时间:2021-02-13 23:13:14

Anyone have any idea why CGContextFillPath won't work in the code snippet below? I'm using the following code to draw to a UIImageView. It's stroking the path correctly, but ignoring CGContextFillPath.

任何人都知道为什么CGContextFillPath不能在下面的代码片段中工作?我正在使用以下代码绘制到UIImageView。它正确地描述了路径,但忽略了CGContextFillPath。

UIGraphicsBeginImageContext(self.frame.size);
[drawingView.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), firstMovedTouch.x, firstMovedTouch.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFillPath(UIGraphicsGetCurrentContext());
drawingView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

4 个解决方案

#1


5  

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.

我认为你需要在填充路径之前调用CGContextClosePath。我不确定你想要绘制什么,但CGContextFillPath将填充路径内的区域,我只在这里看到单行。

#2


26  

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

#3


18  

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

#4


9  

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

#1


5  

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.

我认为你需要在填充路径之前调用CGContextClosePath。我不确定你想要绘制什么,但CGContextFillPath将填充路径内的区域,我只在这里看到单行。

#2


26  

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

#3


18  

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

#4


9  

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