在UIScrollView中嵌入UIImage的免费绘图

时间:2016-12-06 21:25:48

标签: ios swift uiscrollview uiimage core-graphics

我正在研究涉及在UIScrollView内的UIImage上徒手绘制的项目。在默认缩放下绘图时一切正常,但是如果我调整UIScrollView的缩放并再次尝试绘图,它会通过缩小图像并使图像看起来模糊而开始扭曲图像。它还将图像推到屏幕的最左侧。我尝试过使用不同的缩放选项无济于事。

这是正常比例的图像,其中包含一些绘图。enter image description here

这是放大和绘制几秒后的图像 enter image description here

以下是相关代码(方法在包含滚动视图的视图控制器中声明

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)
}

0 个答案:

没有答案