0

I have drawn a straight line on UIImageView. Now I want to remove that drawn line if user taps on it. Here is my code for drawing a line on Image.

- (void)handleLongPressGestures:(UILongPressGestureRecognizer *)sender
{
            if (sender.state == UIGestureRecognizerStateEnded)
            {

       CGPoint  location=[sender locationInView:imageView];
       CGPoint contextLocation = CGPointMake(location.x*imageView.image.size.width/imageView.frame.size.width, location.y*imageView.image.size.height/imageView.frame.size.height);
       UIColor * linearcolor=[UIColor blueColor];
       CGSize imagesize=imageView.image.size;
       UIGraphicsBeginImageContext(imageView.image.size);
       [imageView.image drawAtPoint:CGPointMake(0, 0)];
       CGContextRef context=UIGraphicsGetCurrentContext();
       CGContextSetLineWidth(context, 2.0);
       CGPoint location2=CGPointMake(300, 400);
       CGContextBeginPath(context);
       CGContextMoveToPoint(context, contextLocation.x, contextLocation.y);
       CGContextAddLineToPoint(context, location2.x, location2.y);
       CGContextAddQuadCurveToPoint(context, contextLocation.x, contextLocation.y, location2.x, location2.y);
       CGContextSetLineCap(context, kCGLineCapRound);
       if([removeLine  isEqualToString:@"true"])
       {            
         CGContextClearRect (context, CGRectMake(location2.x, location2.y, contextLocation.x, contextLocation.y));
           CGContextFlush(context); 
      }

       CGContextSetStrokeColorWithColor(context,[linearcolor CGColor]);
       CGContextStrokePath(context); 
       UIImage * newImage=UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();
       removeLine=@"true";
       imageView.image=newImage;

    }
}
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
iOS_Learner
  • 159
  • 9
  • You've drawn on your image, not your image view. You can't undelete a line in your image. But you can save a version of your image before you drew the line so you have something to revert to later. – Lyndsey Scott Jan 15 '15 at 07:02
  • @LyndseyScott you are right I have drawn on image.but If I would follow your suggestion in that case how many times I would have to maintain the previous state of image.I mean If user draws four lines and after that he wants to remove the second one by tapping on it then how I would handle it ...... :( – iOS_Learner Jan 15 '15 at 07:09
  • Oh, I see... You want to do multiple lines. Then perhaps you shouldn't immediately add those lines to the image. Put them on a view with a clear background above the image and only merge with the image once complete. – Lyndsey Scott Jan 15 '15 at 07:12
  • @LyndseyScott actually in my case user can delete any line at any time.Let me explain you with more detail.In my case if user uses the long press gesture at any location , I draw a line from that location to any other hard coded point(location).User can draw multiples. Now if user uses a long press gesture on a certain line then only that line should be removed from the image. This is what I am trying to achieve..... – iOS_Learner Jan 15 '15 at 07:21
  • 1
    "how many times I would have to maintain the previous state of image" only once actually. What you need is to keep the original image, the current image and a set of points you draw on the image. You can use an NSValue to store the points using valueWithCGPoint: static method. So basically you keep the code you have, duplicate the image to store the original. In the gesture method save the locations as well. Then on undo remove the last point pair from the array and redraw all the lines from points in array to a new duplicate of original image. – Matic Oblak Jan 15 '15 at 07:54
  • @MaticOblak I have followed your instructions and its superb ... :) . now I want to delete any line not the last drawn line. I mean if user performs the long press gesture at any line then that specific line should be deleted, rest of the line must be on the image. – iOS_Learner Jan 16 '15 at 10:36
  • @MaticOblak can you please look into this question http://stackoverflow.com/questions/27983655/removing-lines-from-uiimage-in-ios – iOS_Learner Jan 16 '15 at 12:11

0 Answers0