我正在尝试为我的应用程序提供橡皮擦功能(并在将来恢复),但面临一些问题。我正在使用 Swift 4.2,这是我到目前为止所做的:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch?{
let touchPoint = touch.location(in: self.lassoImageView) + lassoOffset!
print("touch begin to : \(touchPoint)")
eraserStartPoint = touchPoint
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch?{
let touchPoint = touch.location(in: self.lassoImageView) + lassoOffset!
print("touch moved to : \(touchPoint)")
erase(fromPoint: eraserStartPoint!, toPoint: touchPoint)
eraserStartPoint = touchPoint
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch?{
let touchPoint = touch.location(in: self.lassoImageView) + lassoOffset!
print("touch ended at : \(touchPoint)")
erase(fromPoint: touchPoint, toPoint: touchPoint)
UIGraphicsBeginImageContext(lassoImageView.frame.size)
lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height), blendMode: .normal, alpha: 1.0)
lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height), blendMode: .normal, alpha: 1.0)
lassoImageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
func erase(fromPoint: CGPoint, toPoint: CGPoint) {
UIGraphicsBeginImageContextWithOptions(lassoImageView.bounds.size, false, 1)
//UIGraphicsBeginImageContext(lassoImageView.bounds.size)
//UIGraphicsBeginImageContext(lassoImageView.image!.size)
let context = UIGraphicsGetCurrentContext()
lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height))
//lassoImageView.image?.draw(in: calculateRectOfImageInImageView(imageView: lassoImageView))
context?.move(to: fromPoint)
context?.addLine(to: toPoint)
context?.setLineCap(.round)
context?.setLineWidth(CGFloat(eraserBrushWidth))
context?.setBlendMode(.clear)
context?.strokePath()
lassoImageView.image = UIGraphicsGetImageFromCurrentImageContext()
croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
lassoImageView // this is UIImageView where I'm loading image from gallery and then erasing image with my finger
lassoOffset // this is Float for touch offset because finger hides part of image
代码很简单并且确实有效,但是如果图像长宽比与 UIImageView 长宽比不同。 UIImageView 内容模式设置为 Aspect Fit
但无论如何,当我拖动手指时,图像会被拉伸(stretch),就像 Scale to Fill
我相信问题出在我的这一行中:
lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height))
我什至尝试计算图像矩形本身,但在这种情况下,图像在触摸开始
后就消失了
也许我的问题很菜鸟,我可能错过了一些荒谬的东西,但我真的真的需要弄清楚这一点。
感谢大家,祝你有美好的一天
最好,维克多
最佳答案
天哪...
我刚刚做了这个
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch?{
let touchPoint = touch.location(in: self.lassoImageView) + lassoOffset!
print("touch ended at : \(touchPoint)")
erase(fromPoint: touchPoint, toPoint: touchPoint)
/*UIGraphicsBeginImageContext(lassoImageView.frame.size)
lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height), blendMode: .normal, alpha: 1.0)
lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height), blendMode: .normal, alpha: 1.0)
lassoImageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext() */
}
并在我的erase
方法中更改了这一行:
lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height))
对于这个:
lassoImageView.layer.render(in: context!)
不确定这里发生了什么,但现在效果很好
关于swift - 尝试快速删除 uiimage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55807240/