Iphone CGContextFillPath(上下文)在创建行时被删除
我在Iphone CGContextFillPath(上下文)在创建行时被删除,iphone,ios,core-graphics,cgcontextref,Iphone,Ios,Core Graphics,Cgcontextref,我在viewDidLoad中使用下面的代码填充路径,效果非常好 UIGraphicsBeginImageContext(_drawingPad.frame.size); CGContextRef context1 = UIGraphicsGetCurrentContext(); CGContextMoveToPoint(context1, 300, 300); CGContextAddLineToPoint(context1, 400, 350); CGContextAddLineToPoin
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);
当触摸开始时,我也在创建一条线。。
但是在我创建线之前,填充路径会被擦除。您试图在不创建路径的情况下绘制路径 请尝试以下操作:
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
替换
CGContextFillPath(context1);
CGContextStrokePath(context1);
借
这将填充并划过当前路径,而不会在其间擦除路径。OP正在当前图形上下文中创建路径。您的代码首先创建一个单独的CGMutablePathRef,然后将其添加到当前上下文中。这当然也是有效的,但(我假设)与OP的问题无关。我当然可能错了:-)谢谢你的答复。。但当我为CGContextRef创建另一个对象时,问题再次出现。。旧路被抹去。
CGContextDrawPath(context1, kCGPathFillStroke);