【问题标题】:clear the drawing on UIImage?清除 UIImage 上的绘图?
【发布时间】:2013-09-20 05:18:38
【问题描述】:

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

非常感谢任何帮助!!!!!!

【问题讨论】:

  • 非常感谢任何帮助!!!!!!

标签: iphone ios ios5 ios6.1 ios6.0


【解决方案1】:

简单的解决方案 - 在开头保存原始图像,当您想擦除线条时,只需这样做:

self.image = savedImage

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多