2013-08-12 30 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創建另一個對象時,問題再次發生。舊路徑被擦除。 –