【发布时间】:2017-03-12 08:15:49
【问题描述】:
我设法在 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。此绘图代码由平移手势识别器(更改状态)触发。它基本上是跟随手指(用手指绘制/擦除)。
【问题讨论】:
-
你能不能告诉观众,
alpresp 的价值是什么?_brushAlpha是?但是,这个绘图代码在哪里? -
好的,我为代码添加了更多详细信息。
标签: ios objective-c alpha cgcontext erase