I draw the lines according to the touchesMoved:
method, and it usually works fine. But when I enlarge the image and draw, the previously drawn lines shift and become more blurry, eventually disappearing. I tried using UIPinchGestureRecognizer
and just increased the frame
of myImageView
(only for multi-touch events), but the problem occurs in both directions. Here's the code to draw:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSArray *allTouches = [touches allObjects]; int count = [allTouches count]; if(count==1){//single touch case for drawing line UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:myImageView]; UIGraphicsBeginImageContext(myImageView.frame.size); [drawImage.image drawInRect:CGRectMake(0, 0, myImageView.frame.size.width, myImageView.frame.size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0); CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); CGContextStrokePath(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); lastPoint = currentPoint; } else{//multi touch case // handle pinch/zoom } }
Here is an image drawn without scaling:
And this is an image depicting the problem after scaling with a red arrow showing the segment that was already drawn before scaling (as shown in the previous image). Image is blurry and offset:
You can also notice that the part of the line drawn to the end is not affected, and the phenomenon occurs for the lines pushed back. I believe the reason for this is because the image size attributes are lost when I zoom in / out, which probably causes blurring and shifting, but I'm not so sure!
EDIT . I uploaded a short video to show what was happening. This is kind of entertaining ...
EDIT 2 - Here 's a one-view sample application focusing on a problem.
source share