试图快速擦除uiimage

时间:2019-04-23 08:34:49

标签: swift uiimage crop erase uigraphicscontext

我正在尝试为我的应用程序提供橡皮擦功能(并在将来还原),但遇到了一些问题。我正在使用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,但无论如何,当我拖动手指图像时,其拉伸程度就好像Scale to Fill

我认为这是我的直肠问题:

lassoImageView.image?.draw(in: CGRect(x: 0, y: 0, width: lassoImageView.frame.size.width, height: lassoImageView.frame.size.height))

我什至尝试计算图像本身,但在这种情况下,图像在touches begun之后就消失了

也许我的问题是菜鸟,我可能错过了一些荒谬的东西,但我确实真的需要弄清楚这一点。

感谢大家,祝您有愉快的一天

最好的维克多

1 个答案:

答案 0 :(得分:0)

哦,我的...

我刚刚做这个

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

不确定此处会发生什么,但现在效果很好