0

我绘制了 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;

任何帮助深表感谢!!!!!!

4

1 回答 1

1

简单的解决方案 - 在开始时保存原始图像,当您想要擦除线条时,只需执行以下操作:

self.image = savedImage

但是,更好的解决方案(具有更好的性能)是根本不更改图像,而是在图像上具有透明视图并在其中UIImageView绘制。绘图也可以简单得多(例如,使用CGImageRef缓冲区而不是每次都创建图像上下文UIGraphicsBeginImageContext)。

于 2013-09-20T05:23:19.157 回答