我成功地在 CGContext 上实现了删除绘图
UIImageView *maskImgView = [self.view viewWithTag:K_MASKIMG];
UIGraphicsBeginImageContext(maskImgView.image.size);
[maskImgView.image drawAtPoint:CGPointMake(0,0)];
float alp = 0.5;
UIImage *oriBrush = [UIImage imageNamed:_brushName];
//sets the style for the endpoints of lines drawn in a graphics context
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat eraseSize = oriBrush.size.width*_brushSize/_zoomCurrentFactor;
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineJoin(ctx, kCGLineJoinRound);
CGContextSetLineWidth(ctx,eraseSize);
CGContextSetRGBStrokeColor(ctx, 1, 1, 1, alp);
CGContextSetBlendMode(ctx, kCGBlendModeClear);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, lastPoint.x,lastPoint.y);
CGPoint vector = CGPointMake(currentPoint.x - lastPoint.x, currentPoint.y - lastPoint.y);
CGFloat distance = hypotf(vector.x, vector.y);
vector.x /= distance;
vector.y /= distance;
for (CGFloat i = 0; i < distance; i += 1.0f) {
CGPoint p = CGPointMake(lastPoint.x + i * vector.x, lastPoint.y + i * vector.y);
CGContextAddLineToPoint(ctx, p.x, p.y);
}
CGContextStrokePath(ctx);
maskImgView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
问题是,这完全删除了任何内容。此函数中设置的 alpha (CGContextSetRGBStrokeColor(ctx, 1, 1, 1, alp);
) 似乎被完全忽略。
我想轻轻删除,然后重复删除就会完全删除绘图。
有什么想法吗?
编辑:根据请求,我添加有关此代码的更多详细信息: alp=_brushAlpha 是此 ViewController 类中声明的属性。其范围为 0.1 至 1.0。在测试时我将其设置为 0.5。该绘图代码由平移手势识别器(更改状态)触发。它基本上是跟随手指(用手指绘制/删除)。
最佳答案
您已将混合模式设置为透明。这忽略了描边颜色。您应该尝试一下各种模式,但我怀疑您想要 sourceAtop 或 screen 之类的东西。查看CGBlendMode docs了解全部详情。
关于ios - 如何轻轻删除cgcontext中绘制的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42745223/