通过cgcontextref来画图的时候,实现橡皮擦功能的方式有两种:
1. 将画笔设置为背景色来实现橡皮擦功能。
这种情况适用于当前设置context的blend mode为normal的时候,即
CGContextSetBlendMode(context, kCGBlendModeNormal);
此时设置背景色的代码如下:
UIGraphicsBeginImageContext ( drawBgView . bounds . size ); // get the image [ drawBgView . layer renderInContext: UIGraphicsGetCurrentContext ()]; UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); self . drawView . currentColor = [ UIColor colorWithPatternImage: image ];
2.通过clearColor来实现橡皮擦功能
个人觉得第二种方式更加简单方面且适用性更加广泛。
第一种情况如果背景色可以变化的情况下,则会有问题。第二种方式因此更具有通用性。
CGContextSetBlendMode(context, kCGBlendModeCopy);
此时再设置画笔颜色为clearColor就可以清除画笔
WARNING:设置blend mode为copy的情况下,如果画笔同时支持透明度的调整,则会出现一些小问题,需要注意。