我画了一部分UIButtons图像,点击清除选项后我需要将按钮图像重置为第一个(删除图纸)。我用setImage属性设置了UIButton的图像,但是它没有工作,如何从图像中清除图形? 我使用以下代码在UIImage上绘图,
UIImageView *imgView =[[UIImageView alloc]init];
UIGraphicsBeginImageContext(self.frame.size);
[imgView.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), self.drawingWidth);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(),lipColor.CGColor);
CGContextSetAlpha(UIGraphicsGetCurrentContext(), self.currntAlpha);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeOverlay);
imgView.image = UIGraphicsGetImageFromCurrentImageContext();
imgView.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);// Set frame for imageView
[self.imageView addSubview:imgView];
UIGraphicsEndImageContext();
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.bounds);
imgView.layer.shadowOffset = CGSizeZero;
imgView.layer.masksToBounds = NO;
imgView.alpha=0.05;
lastPoint=currentPoint;
self.layer.shadowOpacity=0.01;
非常感谢任何帮助!!!!!!
答案 0 :(得分:1)
简单的解决方案 - 在开始时保存原始图像,当您想要删除线条时:
self.image = savedImage
然而,更好的解决方案(具有更好的性能)将是根本不改变图像,而是在UIImageView
上具有透明视图并在其中绘制。绘图也可以更简单(例如,使用CGImageRef
缓冲区而不是每次UIGraphicsBeginImageContext
创建图像上下文。)