为绘图应用程序实现橡皮擦功能 [英] Implementing Eraser functionality for drawing app

查看:95
本文介绍了为绘图应用程序实现橡皮擦功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在ray wenderlich的网站上关注此精彩教程,内容涉及如何使用UIKit创建简单的绘图应用程序。
http:// www .raywenderlich.com / 18840 / how-to-make-a-simple-drawing-app-with-uikit

I was following this great tutorial at ray wenderlich's site about creating a simple drawing app with UIKit. http://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit

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

the tutorial is great and everything is working. the problem that I have is that when it came to creating the eraser functionality the solution proposed by ray was to use the brush with the same color that the background has. To me this doesn't seem like a great solution. what if the background is not a solid color like a gradient or any image like in so many coloring book apps.

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

So basically the question is: is there a way to remove color (convert all pixels in that area to transparent maybe) from a UIImageView at a given location?

任何帮助或指针都将得到极大的应用。谢谢

Any help or pointers would greatly be appriciated. Thanks

推荐答案

经过长时间搜索后,我发现我的应用程序中存在相同的问题。

I have the same issue in my app after the long search i found the simple solution for this issue.

我只是使用了 UIViewController

是我的方法

代码:-

   #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)];
}

这篇关于为绘图应用程序实现橡皮擦功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆