How to draw a transparent stroke (or anyway clear part of an image) on the iPhone

devguy picture devguy · Mar 10, 2009 · Viewed 13.1k times · Source

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

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!

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

Answer

Northorn picture Northorn · Apr 27, 2010

This will do the trick:

CGContextSetBlendMode(context, kCGBlendModeClear)