Reputation: 41
In one of my apps, am trying to erase/transparent stroke a part of UIImage
which am drawing using CoreGraphics framework (CGContextRef
etc..). Well in the process I am able to clear the drawing in one shot by calling "removeAllObjects" message, but I was not able to figure out, how to erase a part of the drawing image. Gosh!! I sat the whole day but still no results, now its killing me. Please guys help me out from here. In short, my requirement is something like an eraser which can erase a part of of my drawing image. Appreciate your help!!
Upvotes: 4
Views: 1637
Reputation: 1069
Just do CGContextSetBlendMode(context, kCGBlendModeClear)
This call changes the color blending mode in such a way that drawing operations would be clearing bitmap instead of drawing with color.
To switch back to normal rendering do CGContextSetBlendMode(context, kCGBlendModeNormal)
Using different blending modes can be very helpful.
Upvotes: 5
Reputation: 32346
Generally, CGContext
s are additive-only canvases--that is you can only add more paint, not remove any existing paint that you've put on them. If your canvas was white to start with, you can paint over with white to fake and erase operation.
A simple solution would be to manually clear out the areas of the backing bitmap are being erasing.
A complex solution is to store all the painting operations that the user has entered and deform them as a result of the erase operation (this will also make adding an undo mode simple)
Note: There is the CGContextClearRect
function, but it generally only works as expected on bitmap contexts.
Upvotes: -1