想在Quartz的ipad绘画应用程序中添加手动擦除选项

时间:2010-10-05 13:11:50

标签: iphone ipad

我正在使用Quartz 2D for ipad进行绘画应用。现在我想添加一个橡皮擦选项,以便用户可以手动擦除他绘制的部分线条。我也有背景图像。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:4)

是的,这在我的应用程序中运行良好; - )

首先,您在触摸开始时添加此代码

UITouch *touch = [touches anyObject];
        lastPoint = [touch locationInView:imgview];
        UIGraphicsBeginImageContext(imgview.frame.size);
        [imgview.image drawInRect:CGRectMake(0, 0, imgview.frame.size.width, imgview.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
        CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:img].CGColor);
        CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor clearColor].CGColor);
        CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
        CGContextBeginPath(UIGraphicsGetCurrentContext());

这里img是你的背景图片然后在移动触摸你简单的写行代码,这是代码是这个

UITouch *touch = [touches anyObject];
        CGPoint currentPoint = [touch locationInView:imgview];
        NSLog(@"asdasdas");
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        imgview.image = UIGraphicsGetImageFromCurrentImageContext();
        lastPoint = currentPoint;

然后你看到最终结果是产生; - )

答案 1 :(得分:0)

if(erase)
    {
        UITouch *touch = [touches anyObject];
        CGPoint currentTouch = [touch locationInView:extraImageVw];

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

        UIGraphicsBeginImageContext(extraImageVw.frame.size);

        CGContextRef context = UIGraphicsGetCurrentContext();
        [extraImageVw.image drawInRect:CGRectMake(0, 0, extraImageVw.frame.size.width, extraImageVw.frame.size.height)];
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, brushSize);
        if (isEraser) {
            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:fullPathToFile]].CGColor);
        }
        else
        {
            CGContextSetStrokeColorWithColor(context, strokeColor);
            CGContextSetBlendMode(context, kCGBlendModeClear);
        }
        CGContextSaveGState(UIGraphicsGetCurrentContext());
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, Lastpoint.x, Lastpoint.y);
        CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
        CGContextStrokePath(context);
        extraImageVw.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        Lastpoint = [touch locationInView:extraImageVw];


    }