我正在研究一个照片应用程序,您可以在照片上绘制线条。 背景是照片,并通过drawrect我在子视图上画线。如何在子视图中擦除线条
每一件事情都很有效,但我怎么擦除子视图上的线条(比如橡皮擦),我试着用clearColor作为setStroke。
是否可以擦除这样的线条。
我正在研究一个照片应用程序,您可以在照片上绘制线条。 背景是照片,并通过drawrect我在子视图上画线。如何在子视图中擦除线条
每一件事情都很有效,但我怎么擦除子视图上的线条(比如橡皮擦),我试着用clearColor作为setStroke。
是否可以擦除这样的线条。
由@omz给出的响应的一种实现可以是:
(假设imgBlank
是放置在的ViewController的主视图的顶部上的UIImageView
)
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.imgBlank];
NSValue *valCurrPoint=[NSValue valueWithCGPoint:CGPointMake(currentPoint.x, currentPoint.y)];
[self.dots addObject:valCurrPoint];
[self performSelector:@selector(draw:) withObject:valCurrPoint afterDelay:0];
}
-(void)draw:(NSValue*)pointz{
CGPoint point=[pointz CGPointValue];
UIGraphicsBeginImageContextWithOptions(self.imgBlank.frame.size,NO,0);
[self.imgBlank.image drawInRect:CGRectMake(0, 0, self.imgBlank.frame.size.width, self.imgBlank.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),tickness);//set the tickness here
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0f, 1.0f, 1.0f, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.imgBlank.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_lastPoint = point;
}
This works great,thank you – Martin
保存!谢谢。 –
如果要使用完全透明的颜色进行绘制以产生任何效果,则必须将混合模式设置为kCGBlendModeCopy
。
这通常通过CGContextSetBlendMode
函数完成。如果您使用UIBezierPath
进行绘图,则还可以使用strokeWithBlendMode:alpha:
方法。
感谢您的回答 – Martin
我试图使用@ OMZ的的第二个建议在UIBezierPath中使用strokeWithBlendMode:alpha来做到这一点:它工作得很好。下面是代码:
if (pen) {
[self updateColorFromPen];
[path setLineWidth:3.0];
} else if (eraser) {
[[UIColor clearColor] setStroke];
[path setLineWidth:42.0];
[path strokeWithBlendMode:kCGBlendModeCopy alpha:1];
}
请访问以下链接 http://stackoverflow.com/questions/3863931/want-to-add-manual-erasing-option-in-ipad-painting -application-by-quartz/12797513#12797513 –