0

I am working on a painting application using Quartz 2D for ipad. Now I want to add an eraser option so that user can manually erase portion of his drawn line with touch.I have also background image. Can anyone please help?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
outsider
  • 31
  • 2
  • 7
  • what do you have implemented so far? Is there an specific problem you are having, or are you just looking for general advice? – filipe Oct 05 '10 at 13:27
  • possible duplicate of [How to draw a transparent stroke (or anyway clear part of an image) on the iPhone](http://stackoverflow.com/questions/629409/how-to-draw-a-transparent-stroke-or-anyway-clear-part-of-an-image-on-the-iphone) – Brad Larson Oct 05 '10 at 14:04
  • See also [Issue with erase or transparent stroke using CoreGraphics framework](http://stackoverflow.com/questions/3247491/issue-with-erase-or-transparent-stroke-using-coregraphics-framework) – Brad Larson Oct 05 '10 at 14:05
  • @g-azam Please don't add irrelevant tags. This question has nothing to do with Swift. Maybe you were thinking about "iOS" instead? – Eric Aya Aug 21 '16 at 16:07

2 Answers2

4

yes this is work well in my app ;-)

firstly you add this code in touch began

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());

here img is ur background image then on move touch you simple write the line stroke code that code is this

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;

then you see the final result is produce ;-)

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Waseem Shah
  • 2,219
  • 23
  • 23
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];


    }
Dhara
  • 4,093
  • 2
  • 36
  • 69