0

我正在尝试创建一个路径,然后立即检索并修改它。问题是路径被正确创建和显示,但是我无法检索它并进一步修改它。

更新:问题是当我在第二个函数中调用 CGContextClosePath 时,编译器返回此错误 ::CGContextClosePath:没有当前点。 它基本上不会“识别”旧路径,因此无法关闭它。任何建议将不胜感激!谢谢!

这是我的代码:

//Defined at the very beginning outside the functions
CGMutablePathRef _path;

//First function
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];

UIGraphicsBeginImageContext(self.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();

[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 

CGContextSetLineWidth(UIGraphicsGetCurrentContext(), BrushSize);

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0);  
_path = CGPathCreateMutable();

CGContextAddPath (context, _path);

CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);

CGContextStrokePath(context);

drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


//Second function
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();

[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), BrushSize); //1.0); // Brush size

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBFillColor(context, 1.0, 0.4, 1.0, 1.0);

CGPathRef pathFill = CGPathCreateCopy (_path);
CGContextAddPath (context, pathFill);

CGContextClosePath(context);
CGContextFillPath(context);

drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
4

1 回答 1

0

尝试在描边之前再次将路径添加到上下文中。

CGContextAddPath (UIGraphicsGetCurrentContext(), _path);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

如果它不起作用,请尝试创建原始路径的副本并在其中添加新行,而不是尝试使用原始路径。利用:

CGPathCreateCopy
于 2012-08-11T14:40:25.963 回答