【问题标题】:Implementing Eraser functionality for drawing app为绘图应用程序实现橡皮擦功能
【发布时间】:2013-04-02 19:51:39
【问题描述】:

我在 ray wenderlich 的网站上关注这个关于使用 UIKit 创建简单绘图应用程序的精彩教程。 http://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit

教程很棒,一切正常。我遇到的问题是,在创建橡皮擦功能时,ray 提出的解决方案是使用与背景颜色相同的画笔。对我来说,这似乎不是一个很好的解决方案。如果背景不是像渐变那样的纯色或像许多涂色书应用程序中那样的任何图像怎么办。

所以基本上问题是:有没有办法从给定位置的 UIImageView 中删除颜色(可能将该区域中的所有像素转换为透明)?

任何帮助或指针将不胜感激。谢谢

【问题讨论】:

标签: iphone ios uiimageview drawing


【解决方案1】:

经过长时间的搜索,我在我的应用中遇到了同样的问题,我找到了解决这个问题的简单方法。

我只是用了UIViewController的touches方法

以下是我的方法,

代码:-

   #pragma mark touches stuff...



 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        lastTouch = [touch locationInView:self.editedImageView];
    }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        currentTouch = [touch locationInView:self.editedImageView];

        CGFloat brushSize;
        if (isEraser) 
        {
            brushSize=eraser;
        }
        else
        {
            brushSize=mark;
        }
        CGColorRef strokeColor = [UIColor whiteColor].CGColor;

        UIGraphicsBeginImageContext(self.editedImageView.frame.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.editedImageView.image drawInRect:CGRectMake(0, 0, self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)];
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, brushSize);
        if (isEraser) {

            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:self.im].CGColor);
        }  
        else
        {
            CGContextSetStrokeColorWithColor(context, strokeColor);
            CGContextSetBlendMode(context, kCGBlendModeClear); 
        }
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
        CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
        CGContextStrokePath(context);
        self.editedImageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        lastTouch = [touch locationInView:self.editedImageView];
    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {

    }

输入:

self.editedImageView.image = "Your Custom image";

self.im    =   "Your Custom image";

您的问题的简单解决方案是:-

CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:self.im].CGColor);

注意:

这不会取消它再次绘制图像

更新:-

 self.im    =   "Your Custom image";

这将是这样的......

-(void)eraserConfigure
{
    UIImageView *resizeImage=[[[UIImageView alloc]initWithImage:editedImage]autorelease];
    /*UIImage */self.im=[UIImage imageFromView:resizeImage scaledToSize:CGSizeMake(self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)];
}

【讨论】:

  • 注意:如果您的背景是白色或其他颜色,这不是擦除而是重新绘制相同的情况,然后使用该颜色或图像意味着使用上面的代码
  • 太棒了...不得不与代码斗争一点..我的有点太乱了,但终于让它工作了..谢谢
  • @UmerHassam 谢谢,遇到任何问题请告诉我
【解决方案2】:

使用画笔但用于颜色:

[UIColor clearColor]

【讨论】:

  • 尝试改用 CGContextSetStrokeColorWithColor。 CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor)
【解决方案3】:

我可以使用以下代码解决问题:

CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);

Reference

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 2012-03-02
    • 2014-05-14
    相关资源
    最近更新 更多