为iOS中的绘画应用程序制作橡皮擦工具

为iOS中的绘画应用程序制作橡皮擦工具,ios,paint,erase,brush,Ios,Paint,Erase,Brush,我正在创建一个绘画应用程序,我想知道如何实现橡皮擦工具。我不想让我的橡皮擦工具画白色,因为我想让用户改变背景色。还有,是否可以设置刷子的硬度?如果是,请告诉我怎么做 多谢各位 以下是我迄今为止所做的工作: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; lastPoint = [touch locationInView:

我正在创建一个绘画应用程序,我想知道如何实现橡皮擦工具。我不想让我的橡皮擦工具画白色,因为我想让用户改变背景色。还有,是否可以设置刷子的硬度?如果是,请告诉我怎么做

多谢各位

以下是我迄今为止所做的工作:

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

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    UIGraphicsBeginImageContext(self.view.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;
}
- (IBAction)clear:(id)sender {
drawImage.image = nil;
}

我已经制作了绘画应用程序,它可以在iTunes“Easy Doodle”应用程序上使用。橡皮擦并没有特殊的逻辑。只需获取背景图像的RGB值并将其传入即可

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),R value,G value,B value, 1.0);

根据选择的背景图像。

好的,下面是我对橡皮擦工具所做的操作:
我添加这行代码:
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear)

所以代码应该是这样的:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

// I add this
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), sizeSlider.value);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);

CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}

好的,我一直在寻找一个解决方案,如何创建一个橡皮擦现在一个星期了,我必须改变我看它的方式,因为无论我多少次试图擦除一些东西,它也会擦除背景图像

然而,最后我确实找到了一个适合我的解决方案(我可以擦除油漆,并且仍然可以感觉到背景图像未被触摸或擦除),希望它能帮助其他人。 所以这里是

1) 在UIView中创建2个ImageView 2) 将两个ImageView设置为所需的背景图像。。。 3) 我创建了一个分段控件来确定用户是否要删除 4) 选择分段控件时添加了以下功能

  func drawLineForRubber(fromPoint: CGPoint, toPoint: CGPoint) {

    // 1
    UIGraphicsBeginImageContext(view.frame.size)
    let context = UIGraphicsGetCurrentContext()
    mainImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))

    // 2
    CGContextMoveToPoint(context, fromPoint.x, fromPoint.y)
    CGContextAddLineToPoint(context, toPoint.x, toPoint.y)

    // 3
    CGContextSetBlendMode(context, CGBlendMode.Clear)
    CGContextSetLineCap(context, CGLineCap.Round)
    CGContextSetLineWidth(context, 10.0)
    //this line is very important as it paints the screen clear
    CGContextSetRGBStrokeColor(context, red, green, blue, 0.0)
    CGContextSetBlendMode(context, CGBlendMode.Clear)

    // 4
    CGContextStrokePath(context)

    // 5
    mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    mainImageView.alpha = opacity
    UIGraphicsEndImageContext()

}
正如您将看到的,其中有一行将CGContextSetRGBStrokeColor的值设置为0.0,这是一个重要原因,当用户擦除绘制时,他将从顶部图像视图中擦除绘制以及具有透明颜色的绘制。但是,由于它下面有另一个imageview,因此感觉是它看起来像是被擦掉了,而不会影响背景图像

相反,它被擦掉了,但后面的图像视图使它看起来好像没有。当您想要导出图像时(您可以通过多种方式进行导出,但我采用了以下方法),您已经使用背景图像进行了绘制,只需将两个图像视图合并为背景图像即可 First imageview将具有您绘制的油漆和 第二个图像视图将具有您想要的原始背景图像

    func share() {

    let layer = UIApplication.sharedApplication().keyWindow!.layer
    let scale = UIScreen.mainScreen().scale
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale); // reconsider size property for your screenshot

    layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()


    UIGraphicsBeginImageContext(mainImageView.bounds.size)
    mainImageView.image?.drawInRect(CGRect(x: 0, y: 0,
        width: mainImageView.frame.size.width, height: mainImageView.frame.size.height))

    UIGraphicsEndImageContext()

    let activity = UIActivityViewController(activityItems: [screenshot], applicationActivities: nil)
    presentViewController(activity, animated: true, completion: nil)
}

希望这能帮助其他人。。老兄,我花了好长时间才弄明白这是一个有趣的问题,但我想你可以说得更清楚。告诉我们你到目前为止都做了些什么;可能包括一些代码。@jtbandes我已经添加了代码,请查看此链接并查看我的答案@mat hi。。如果我们为绘制创建自定义UIView,那么在没有drawrect mehod的情况下我们可以绘制吗?可能您想问Rock,但我不这么认为,如果您有自定义UIView,则需要调用setNeedsDiplay,以便在每次修改视图时绘制视图本身。否则,看看我的旧的。这不是正确的答案。正确答案是下面的答案,上面的票数更多。苹果有一个内置的橡皮擦功能,这是一行代码。这个答案甚至没有实际擦除,它只是在图像中添加了更多的“颜料”,只是与它下面的像素(背景图像)颜色相同。不是真的擦除(特别是如果背景图像改变)和更多的数据重。非常感谢@Mat!谢谢。它工作得很好。我已经找了很久了。