缩放和绘制图像时CGContextStrokePath无法正常工作

tip*_*low 6 iphone core-graphics uiimage pinch ios

我正在根据touchesMoved:方法绘制线条,通常它工作正常.但是当我放大图像并绘制时,先前绘制的线条都会移位并且越来越模糊,最终消失.我已经尝试使用UIPinchGestureRecognizer,只是增加framemyImageView(仅适用于多点触摸事件),但出现问题两种方式.这是绘图的代码:

- (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
  }
}
Run Code Online (Sandbox Code Playgroud)

这是在没有缩放的情况下绘制的图像:

在此输入图像描述

这是用放大后用红色箭头表示已经在放大之前绘制的片段(如上图所示)中描述问题的图像.图像模糊不清:

在此输入图像描述

还可以注意到,朝向末端绘制的线的一部分不受影响,并且对于及时拉回的线发生现象.我相信这样做的原因是当我放大/缩小时图像尺寸属性会丢失,这可能会导致模糊和移位,但我不确定!

编辑 - 我上传了一段短片来展示正在发生的事情.这有点娱乐......

编辑2-这是一个侧重于该问题的示例单视图应用程序.

小智 4

我下载了你的项目,发现问题是自动调整大小。以下步骤将解决它:

步骤 1.注释第 70 行:

drawImage.frame = CGRectMake(0, 0, labOrderImgView.frame.size.width, labOrderImgView.frame.size.height);
Run Code Online (Sandbox Code Playgroud)

在你的touchesMoved方法中。

步骤2.alloc在方法中的drawImage被编辑后(第90行)添加一行代码viewDidLoad

drawImage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Run Code Online (Sandbox Code Playgroud)

然后,该错误就被修复了。