2012-08-11 55 views
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(); 

回答

0

尝试中风你之前再次添加路径上下文中。

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

它不起作用,尝试创建原始路径的副本,并添加一个新行,而不是试图使用原来的。使用:

CGPathCreateCopy 
+0

它不工作... – jeddi 2012-08-12 07:00:32

+0

谢谢,但我不能得到它的工作(请参阅上面编辑的问题)。欢呼 – jeddi 2012-08-13 21:48:14

+0

我不能使用这种方法,因为最终目的是填补封闭的路径... – jeddi 2012-08-14 11:28:20