Remove Line from UIImageView

纵饮孤独 提交于 2019-12-12 05:27:32

问题


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;

    }
}

来源:https://stackoverflow.com/questions/27958089/remove-line-from-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!