c语言擦除图形,关于物镜c:通过触摸绘制和擦除线条

我在单独的UIImageViews中有一个背景图像(ImageBG)和一个前景图像(ImageFG)。 它们的大小相同,顾名思义,ImageBG位于ImageFG的后面,因此您看不到ImageBG。

如果用户开始在其上"绘制",而不是在用户触摸屏幕的地方出现线条,我希望ImageFG的该部分变得透明并显示ImageBG。

我能想到的最接近的是刮刮取胜。

我知道如何在图形上下文中进行绘制,但是当我绘制一条透明线(alpha = 0)时,...在我的图形上下文中当前存在的任何内容之上都得到一条透明线,因此基本上没有绘制任何内容 。 这就是我用来设置笔触颜色的方法。

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 0.0)

我究竟做错了什么? 我知道这是可能的,因为那里有应用程序可以做到这一点。

任何提示,技巧或方向表示赞赏。

因此,您有一个层,该层是一些像要刮掉的灰色东西一样的图像,您可以将此图像(在此示例中为UIImageView及其UIImage .image)写入图形上下文。 然后将混合模式设置为kCGBlendModeClear对其进行描边,然后保存该图像(具有清除的路径)。 这将显示您可能在另一个UIImageView中的图像。 请注意,在这种情况下,self是UIView。

因此,在touchesMoved外部创建了一个变量来保存CGPoint

CGPoint previousPoint;

CGPoint currentPoint;

然后开始触摸,将其设置为上一点。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

previousPoint = [touch locationInView:self];

}

在touchesMoved中,将图像写入上下文,使用透明混合模式描边图像,从上下文中保存图像,并将上一个点设置为当前点。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

currentPoint = [touch locationInView:self];

UIGraphicsBeginImageContext(self.frame.size);

//This may not be necessary if you are just erasing, in my case I am

//adding lines repeatedly to an image so you may want to experiment with moving

//this to touchesBegan or something. Which of course would also require the begin

//graphics context etc.

[scratchImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

CGContextSaveGState(UIGraphicsGetCurrentContext());

CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.25, 0.25, 0.25, 1.0);

CGMutablePathRef path = CGPathCreateMutable();

CGPathMoveToPoint(path, nil, previousPoint.x, previousPoint.y);

CGPathAddLineToPoint(path, nil, currentPoint.x, currentPoint.y);

CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

CGContextAddPath(UIGraphicsGetCurrentContext(), path);

CGContextStrokePath(UIGraphicsGetCurrentContext());

scratchImage.image = UIGraphicsGetImageFromCurrentImageContext();

CGContextRestoreGState(UIGraphicsGetCurrentContext());

UIGraphicsEndImageContext();

previousPoint = currentPoint;

}

这有帮助吗? 如果是这样,那么您接受它会很棒,这样其他人就可以知道它是否是正确的答案。

这会删除图像视图中的实际图像,而不是在其上绘制的图形。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值