【发布时间】:2025-12-26 13:45:07
【问题描述】:
我有一个 UIView,我有绘制图像。在这个视图中,我正在绘制钢笔工具、矩形工具并更新新图像并进行绘制。就像ACEDrawingView
我在使用橡皮擦工具时遇到了一个问题。当我使用橡皮擦时,背景图像颜色也消失了。请检查屏幕截图。
在上图中,黑色部分是钢笔画,白色部分是我的橡皮擦画。当用户绘制橡皮擦不清除背景图像时,我想要什么。只是绘图。
重绘图像代码
- (void)updateCacheImage:(BOOL)redraw
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
if (redraw) {
// erase the previous image
self.image = nil;
// load previous image (if returning to screen)
[[self.prev_image copy] drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
// I need to redraw all the lines
for (id<ACEDrawingTool> tool in self.pathArray) {
[tool draw];
}
} else {
// set the draw point
[self.image drawAtPoint:CGPointZero];
[self.currentTool draw];
}
// store the image
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
橡皮擦工具绘图
- (void)draw
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context, self.path);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextSetAlpha(context, self.lineAlpha);
CGContextStrokePath(context);
}
【问题讨论】: