Thanks for your time.
The image is fine before I tap the image, and then the image is compacted after I tap it.
Here is my code:
I am not using storyboard, so I create everything with code, here is the ImageView. And I added the constraints with code as well.
let imageEditingView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
return imageView
}()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if touches.first != nil {
lastPoint = (touches.first?.location(in: imageEditingView))!
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if touches.first != nil {
let currentPoint = touches.first?.location(in: imageEditingView)
drawLines(fromPoint: lastPoint, toPoint: currentPoint!)
lastPoint = currentPoint!
drawLines(fromPoint: lastPoint, toPoint: lastPoint)
}
}
func drawLines(fromPoint: CGPoint, toPoint: CGPoint) {
UIGraphicsBeginImageContext(imageEditingView.frame.size)
imageEditingView.image?.draw(in: CGRect(x: 0, y: 0, width: imageEditingView.frame.width, height: imageEditingView.frame.height))
let context = UIGraphicsGetCurrentContext()
context?.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y))
context?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))
context?.setBlendMode(CGBlendMode.normal)
context?.setLineCap(CGLineCap.round)
context?.setLineWidth(CGFloat(Int(120 * lineWidthSliderView.value)))
context?.setStrokeColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 0.01)
context?.strokePath()
imageEditingView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}