3

I have this code to draw lines on image user takes. I have successfully added feature to draw what ever the user feel like drawing on his/her image. But the lines which i draw is not smooth enough. I have tried the answers stack overflow experts suggested to others. But mine still looks rough. And one speciality is that I use pan gesture instead of touch, will that make any problems? Expert advices and codes are needed. Thanks in advance. Happy Coding. !

The lines on the picture looks rough, not smooth

And this is from where I call the drawlineFrom method:

  -(void)drawLineFrom:(CGPoint)from endPoint:(CGPoint)to
    {
    counter++;
    bezierPath = UIBezierPath.bezierPath;

    NSLog(@"UIGestureRecognizerStateChanged here");

    [bezierPath moveToPoint: CGPointMake(from.x, from.y)];


   [bezierPath addLineToPoint:CGPointMake(to.x, to.y)];


    [bezierPath closePath];
    bezierPath.usesEvenOddFillRule = YES;
    box = [CAShapeLayer layer];
    box.frame = CGRectMake(0, 0, 500, 500);
    box.path = bezierPath.CGPath;
    box.strokeColor = [UIColor colorWithRed:0.992 green:0.322 blue:0.212 alpha:1.00].CGColor;
    box.fillColor = [UIColor colorWithRed:0.992 green:0.322 blue:0.212 alpha:1.00].CGColor;
    box.lineWidth = 5;

    [self.myImage.layer addSublayer:box ];

    [layerarray addObject:box];
    NSLog(@"AAAAARRRRTTTT%@",layerarray);

    }



{

    startPoint = [gesture locationInView:self.selfieImage];

    if(gesture.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"Touched here \n :X-%f \n Y-%f",[gesture locationInView:self.selfieImage].x,[gesture locationInView:self.selfieImage].y);
        startPoint = lastPoint;
        lastPoint = [gesture locationInView:self.selfieImage];
        [self drawLineFrom:startPoint endPoint:lastPoint];




    }
Alex
  • 538
  • 3
  • 18
  • The line in your image seems to consist of about 100 coordinates. However, in your code you only how you draw a line between a single pair of coordinates. So it seems that the red color in your image is not coming from a single path but instead from a hundred individual line segments. That would explain why they look so rough. In order to draw a smooth continuous line, you have to use a single bezier path that includes all coordinates. – Codo May 11 '16 at 06:59
  • I am drawing boxes where the user touches. That's how I am drawing it now. Is there any other method? I have tried drawing circles but that gives me distinct circles instead of continous cylindrical paths. – Alex May 11 '16 at 09:38
  • Is there any problems if I use **PanGesture** instead of _**UItouch**_ – Alex May 11 '16 at 09:54
  • 1
    Instead of a single continuous path, you draw hundreds of short line segments. That's not what you want to achieve and the reason it looks so jagged. You have to record all the coordinates and merge them into a single path. This probably means that - on each touch move event - you have to remove the current path from the screen, add one coordinate to it and add it again. – Codo May 11 '16 at 10:58
  • where do you call drawLineFrom ? – Teja Nandamuri May 11 '16 at 12:51
  • I use drawLineFrom method in pan gesture delegates. I have finally found out a way to reach my goal. Thanks to all who helped me suggesting new ideas. Happy coding. – Alex May 20 '16 at 10:16

0 Answers0