ios - 如何轻轻删除cgcontext中绘制的路径?

标签 ios objective-c alpha cgcontext erase

我成功地在 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/

相关文章:

objective-c - 在 iOS 中检索类的属性列表

objective-c - 用长整数制作唯一可读的字符串

objective-c - 在运行时对 ObjectiveC 字符串进行字符串化

language-agnostic - 使用预发布的开发工具进行开发

ios - 如何使用 URL 显示图像?

ios - UIButton 在 iOS7 中好像没有选中状态的标题?

flutter - 使用不透明度会产生错误的脚手架背景颜色

ios - 带有 Core Graphic iPhone 的透明(alpha)渐变

iphone - 将上下文传递给另一个方法

ios - UIScrollView 后面的 UITableView