0

我使用下面的代码填充路径,viewDidLoad效果很好

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

当触摸开始时我也在创建一条线..但是在我创建这条线之前填充路径被删除了..

4

2 回答 2

1

代替

CGContextFillPath(context1);
CGContextStrokePath(context1);

经过

CGContextDrawPath(context1, kCGPathFillStroke);

这将填充描边当前路径,而不会在两者之间擦除它。

于 2013-08-12T11:39:08.880 回答
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
于 2013-08-12T11:37:07.457 回答