根据 touchesMoved:
方法,我正在绘制线条,通常情况下都很正常。但当我缩放图像并且进行绘制时,之前画的线条会发生位移,变得越来越模糊,最终消失。我尝试过使用 UIPinchGestureRecognizer
和简单地增加 myImageView
的 frame
(仅针对多点触控事件),但无论哪种方式都会出现问题。下面是绘图的代码:
- (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- 这是一个关注此问题的样本单视图应用程序。
myImageView
和drawImage
(两者都是UIImageView
)设置它,但它没有起作用:(... - tipycalFlow