IOS:绘制一条删除另一条线的线

时间:2011-12-05 12:14:14

标签: ios xcode drawing

在我的代码中:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;

UITouch *touch = [touches anyObject];   
CGPoint currentPoint = [touch locationInView:drawImage];

UIGraphicsBeginImageContext(drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, a);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;

mouseMoved++;

if (mouseMoved == 10) {
    mouseMoved = 0;
}
}

我可以在drawImage中绘制一条线,但是如果我想绘制一条线来移除另一条线?作为取消其他画线的橡皮擦。有可能吗?

1 个答案:

答案 0 :(得分:10)

如果你想通过绘制一条用透明度替换下面的线条来“擦除”像素,那么你需要使笔触颜色完全透明,同时还要将混合模式更改为kCGBlendModeCopy(使用默认的混合模式,用完全透明的笔触颜色绘制当然没有效果。)

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 0, 0, 0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);

之后,您可以像在代码段中一样绘制“橡皮擦”行(CGContextMoveToPointCGContextAddLineToPointCGContextStrokePath