DDGScreenShot—圖片擦除功能

寫在前面

圖片擦除功能,也是運用圖片的繪製功能,
將圖片繪製後,拿到相應的圖片。當然,有一漲底圖更明顯

所有功能演示

imageimage

實現代碼如下

/**
     ** 用手勢擦除圖片
     - imageView --傳圖片
     - bgView --截圖背景
     */
    public func clearImage(imageView: UIImageView?, rect: CGRect) -> UIImage? {
        if imageView == nil {
            return nil
        }
        //開啓一個位圖上下文
        UIGraphicsBeginImageContextWithOptions((imageView?.bounds.size)!, false, 0.0)
        //把ImageView內容渲染到上下文當中
        let imageCtx = UIGraphicsGetCurrentContext()
        imageView?.layer.render(in: imageCtx!)
        //擦除上下文當中某一塊區域
        imageCtx!.clear(rect)
        //得到新圖片
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        //關閉上下文
        UIGraphicsEndImageContext()

        return newImage
    }

    ### 當然你也可以把圖片繪製過程放入子線程中,再次就不列舉了

具體用法

   //底部圖片
    private lazy var bottomImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "image")
        imageView.frame = CGRect(x: 0, y: 100, width: width, height: width)
        self.view.addSubview(imageView)
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    //要擦除的圖片
    private lazy var clearImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "logo")
        imageView.frame = CGRect(x: 0, y: 100, width: width, height: width)
        imageView.isUserInteractionEnabled = true
        self.view.addSubview(imageView)
        return imageView
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.bottomImageView.isUserInteractionEnabled = false
        let pan = UIPanGestureRecognizer(target: self, action: #selector(DDGClearImageView.clearPan(pan:)))
        self.clearImageView.addGestureRecognizer(pan)
    }
    @objc func clearPan(pan: UIPanGestureRecognizer) {
        //獲取當前手指的點
        let imageView = pan.view as! UIImageView
        let clearPan = pan.location(in: imageView)
        //擦除區域的大小
        let rect = CGRect(x: clearPan.x - 15, y: clearPan.y - 15, width: 30, height: 30)
        let newImage = DDGManage.share.clearImage(imageView: imageView, rect: rect)

        imageView.image = newImage
    }

### 是不是很好用

結束語

此代碼已經上傳到githup[DDGScreenShot](https://github.com/dudongge/DDGScreenShot)
[link](https://github.com/dudongge/DDGScreenShot)
當然這只是這個庫的功能的一小部分
想看更多功能,可以去github上下載,如果對您有幫助,希望您不吝給個star.

歡迎查看DDGScreenShot

其餘功能如下

  1. (一)DDGScreenShot — 複雜屏幕截屏(如view ScrollView webView wkwebView)
  2. (二)DDGScreenShot–iOS 圖片處理–多圖片拼接
  3. (三)DDGScreenShot–iOS 圖片裁剪,切圓角,加邊框,你還用cornerRadius,還有更高級的用法
  4. (五)DDGScreenShot—截取圖片的任意部分
  5. (六)DDGScreenShot —圖片加各種濾鏡高逼格操作
  6. (七)DDGScreenShot —圖片加高斯模糊,老電影效果
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章