我正在研究涉及在UIScrollView内的UIImage上徒手绘制的项目。在默认缩放下绘图时一切正常,但是如果我调整UIScrollView的缩放并再次尝试绘图,它会通过缩小图像并使图像看起来模糊而开始扭曲图像。它还将图像推到屏幕的最左侧。我尝试过使用不同的缩放选项无济于事。
以下是相关代码(方法在包含滚动视图的视图控制器中声明
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
let currentPoint = touches.first!.location(in: drawingImageView)
UIGraphicsBeginImageContextWithOptions(drawingImageView.frame.size, false, 0)
drawingImageView.image?.draw(in: CGRect(x: 0, y: 0, width: drawingImageView.frame.size.width, height: drawingImageView.frame.size.height))
UIGraphicsGetCurrentContext()?.addRect(CGRect(x: currentPoint.x, y: currentPoint.y, width: 1, height: 1))
UIGraphicsGetCurrentContext()?.setLineCap(.round)
UIGraphicsGetCurrentContext()?.setLineWidth(10)
UIGraphicsGetCurrentContext()?.setStrokeColor(colorSelectorButton.backgroundColor!.cgColor)
UIGraphicsGetCurrentContext()?.setBlendMode(.normal)
UIGraphicsGetCurrentContext()?.strokePath()
drawingImageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
}