How to remove space in painting using drawinrect?

96 views Asked by At

i'm using the following code to draw lines over an imageview's image,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2)
    return;
lastPoint = [touch locationInView:imageview];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), 3);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5);
    UIGraphicsBeginImageContext(self.view.frame.size);
    [imageview.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextClearRect(UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 5, 5));
    imageview.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
lastPoint = currentPoint;
}

Output

enter image description here

You can see the gaps/spaces between the dots. I need the output should be like below,

enter image description here

What should I change in my code to remove the space between two dots?
I think, touchesended code is not needed for your review. I changed the CGContextSetLineCap and width, no luck.

1

There are 1 answers

0
MDB983 On

I would change the approach, essentially you're capturing touch points, and not "joining the dots" so to speak. You would be better off using UIBezierPaths for this task.

I answered a question some time back where the op wanted to do something similar but was hitting performance issues. There is code in the answer i posted that pretty much does what you want, completely. Review this post for full sample code, you can pretty much cut and paste the class (I see little value in transcribing). Sample drawing path

If you need more help just let me know