试图用swift擦除uiimage

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了试图用swift擦除uiimage相关的知识,希望对你有一定的参考价值。

我正在尝试为我的应用程序制作橡皮擦功能(并在将来恢复)但面临一些问题。我正在使用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))

我甚至试图计算图像rect本身,但在这种情况下,图像在touches begun后消失了

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

感谢每一个人,祝你有个美好的一天

最好,维克托

答案

天啊...

我刚刚做到了这一点

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的主要内容,如果未能解决你的问题,请参考以下文章

试图在 Swift 5.1 中将 UIImage 转换为 PFFileObject

将UIImage转换为NSData并在Swift中转换回UIImage?

无法在 Swift 中使用时间延迟为 UIImage 设置动画

在for循环中将PFFile数组转换为UIImage返回空数组Swift

UIImage 方向 Swift

从 UIImage (Swift) 获取 CIImage