2013-08-12 87 views
0

我用下面的代码为填充路径viewDidLoad它的作品完美CGContextFillPath(上下文)创建一个行

UIGraphicsBeginImageContext(_drawingPad.frame.size); 
CGContextRef context1 = UIGraphicsGetCurrentContext(); 

CGContextMoveToPoint(context1, 300, 300); 
CGContextAddLineToPoint(context1, 400, 350); 
CGContextAddLineToPoint(context1, 300, 400); 
CGContextAddLineToPoint(context1, 250, 350); 
CGContextAddLineToPoint(context1, 300, 300); 

CGContextClosePath(context1); 
//CGContextStrokePath(context1); 

CGContextSetFillColorWithColor(context1, [UIColor redColor].CGColor); 
CGContextFillPath(context1); 
CGContextStrokePath(context1); 

也是我创建一个线的时候开始接触.. 但将填充路径时被删除在创建线条之前擦除。

回答

0

您正在尝试绘制路径而不创建路径。

尝试以下操作:

UIGraphicsBeginImageContext(_drawingPad.frame.size); 
CGContextRef context1 = UIGraphicsGetCurrentContext(); 

CGMutablePathRef path = CGPathCreateMutable(); 

CGPathMoveToPoint(path,300,300); 
CGPathAddLineToPoint(path,400,350); 
CGPathAddLineToPoint(path,300,400); 
CGPathAddLineToPoint(path,250,350); 
CGPathAddLineToPoint(path,300,300); 

CGPathCloseSubpath(path); 

CGContextSetStrokeColorWithColor(context1, [UIColor blackColor].CGColor); 
CGContextSetFillColorWithColor(context1, [UIColor redColor].CGColor); 


CGContextAddPath(context1,path); 

//Now you can fill and stroke the path 
CGContextFillPath(context1); 
CGContextStrokePath(context1); 

CGPathRelease(path); //free up memory 
+0

OP * *在当前图形上下文中创建路径。您的代码首先创建一个单独的CGMutablePathRef,然后将其添加到当前上下文中。这当然也是有效的,但是(正如我所假设的)独立于OP的问题。我当然可能是错的:-) –

1

更换

CGContextFillPath(context1); 
CGContextStrokePath(context1); 

通过

CGContextDrawPath(context1, kCGPathFillStroke); 

,将填补中风的电流路径没有删除它之间。

+0

感谢您的回复..但是当我为CGContextRef创建另一个对象时,问题再次发生。旧路径被擦除。 –