如何在iPhone上绘制透明笔划(或任何清晰的图像部分)

问题描述:

我有一个小应用程序,允许用户用手指在屏幕上绘图。
我有一个 UIImageView 用户通过创建 CGContextRef 和各种 CG绘制函数。我主要使用函数 CGContextAddLineToPoint

I have a small app that allows the user to draw on the screen with the finger. I have a UIImageView where the user draws, by creating a CGContextRef and the various CG draw functions. I primarily draw strokes/lines with the function CGContextAddLineToPoint

绘制笔画/线条现在我的问题是:
用户可以画出各种颜色的线条。我想让他能够使用橡胶工具用手指删除到目前为止绘制的图像的某些部分。我最初通过使用白色作为笔划(使用 CGContextSetRGBStrokeColor 函数设置)来做到这一点,但它没有用完...因为我后来发现 UIImageView 上的c $ c> UIImage 实际上有一个透明的背景,而不是白色...所以我最终会得到一个白色的透明图像就行了!

Now my issue is this: The user can draw lines of various colors. I want to give him the ability to use a "rubber" tool to delete some part of the image drawn so far, with the finger. I initially did this by using a white color for the stroke (set with the CGContextSetRGBStrokeColor function) but it didn't work out...because I discovered later that the UIImage on the UIImageView actually had a transparent background, not white...so I would end up with a transparent image with white lines on it!

无论如何设置透明笔触颜色还是有其他方法可以当他移动它时,在用户的手指下清除 CGContextRef 的内容?
谢谢

Is there anyway to set a "transparent" stroke color or is there any other way to clear the content of the CGContextRef under the user's finger, when he moves it? Thanks

这样做可以解决问题:

CGContextSetBlendMode(context, kCGBlendModeClear)