Issue with erase or transparent stroke using CoreGraphics framework

孤街醉人 提交于 2019-12-10 16:14: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!!


回答1:


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.




回答2:


Generally, CGContexts 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.



来源:https://stackoverflow.com/questions/3247491/issue-with-erase-or-transparent-stroke-using-coregraphics-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!