我正在根据touchesMoved:
方法画线,通常效果很好。但是当我放大图像并进行绘制时,之前绘制的线条都发生了位移,并且越来越模糊,最终消失了。我已经尝试使用UIPinchGestureRecognizer
并简单地增加frame
of myImageView
(仅适用于多点触控事件),但问题是双向发生的。这是绘图的代码:
- (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
}
}
这是在没有缩放的情况下绘制的图像:
这是放大后描述问题的图像,红色箭头显示在放大之前已经绘制的段(如上图所示)。图像既模糊又移位:
还可以注意到,向末端绘制的线的一部分不受影响,并且在时间线向后绘制时会出现这种现象。我相信这样做的原因是当我放大/缩小时图像尺寸属性会丢失,这可能会导致模糊和移位,但我不确定!
编辑- 我上传了一个简短的视频来展示正在发生的事情。这有点有趣...
编辑 2-这是一个专注于该问题的示例单视图应用程序。