谢谢你的时间。
在我点击图像之前图像很好,然后在我点击图像后压缩图像。
这是我的代码:
我没有使用故事板,所以我用代码创建了所有东西,这里是 ImageView。我也用代码添加了约束。
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()
}