gpt4 book ai didi

iphone - CGContext画线: CGContextFillPath not working?

转载 作者:行者123 更新时间:2023-12-03 18:26:15 28 4
gpt4 key购买 nike

有人知道为什么 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();

最佳答案

要解决此问题,请使用以下代码:

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

关于iphone - CGContext画线: CGContextFillPath not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3808984/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com