Swift 自定义 UIImagePickerController 选取图片后的裁剪器(相机裁剪界面)

今天做项目的时候需要自定义裁切来自相机图库选择的图片,找了一圈资料发现相关的信息甚少。爬了好几个小时的坑,分享给大家~

开始之前忍不住吐槽一手苹果,既然系统提供了选取图片后的裁切功能,让开发者传个CGRect,CGSize进去不应该理所当然吗?搞得我一开始疯狂的查水果的 API Reference,最后得出一个无奈的结论。。。宽高定死,你爱用不用:)。。。难道苹果认为用户对图片的需求大小只有那么一种吗?。。。

其实自定义裁剪器主要就是核心绘图

1.画个遮罩出来

Swift 自定义 UIImagePickerController 选取图片后的裁剪器(相机裁剪界面)_第1张图片
IMG_0369.PNG
  UIGraphicsBeginImageContextWithOptions(UIScreen.main.bounds.size, false, 0)
        
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor)
        context?.fill(UIScreen.main.bounds)
        context?.addRect(CGRect(x: 0, y: (HEIGHT - selectHeight)/2, width: WIDTH , height: selectHeight))
        context?.setBlendMode(.clear)
        context?.fillPath()
        
        
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        let selectarea = UIImageView(image: img)
        selectarea.frame.origin = CGPoint(x: 0, y: 0)
        view.addSubview(selectarea)

2.裁剪图片(核心部分到这里就结束了)

        let rect  = UIScreen.main.bounds
        
        // 记录屏幕缩放比
        let scal = UIScreen.main.scale
        
        // 上下文
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
        
        let context = UIGraphicsGetCurrentContext()
        
        UIApplication.shared.keyWindow?.layer.render(in: context!)
        
        // 截全屏,再截图
        guard let img = UIGraphicsGetImageFromCurrentImageContext()?.cgImage,
            let result = img.cropping(to: CGRect(x: 0, y: (HEIGHT - selectHeight)/2 * scal, width: self.WIDTH * scal, height: selectHeight * scal))   else{
                return nil
        }
        // 关闭上下文
        UIGraphicsEndImageContext()
    }

3.定一个协议来传值

protocol SwiftyPhotoClipperDelegate {
    
    func didFinishClippingPhoto(image:UIImage)
}

4.忘了一步,关于图片的缩放,一个 scrollview ,当他的 maximumZoomScale 和 minimumZoomScale 不同并且实现 viewForZooming 返回缩放视图即可。
代码很简单,没什么好贴的,就贴一个定位代码吧

func scrollViewDidZoom(_ scrollView: UIScrollView) {
      
      //当捏或移动时,需要对center重新定义以达到正确显示位置
      var centerX = scrollView.center.x
      var centerY = scrollView.center.y
      centerX = scrollView.contentSize.width > scrollView.frame.size.width ? scrollView.contentSize.width / 2 : centerX
      centerY = scrollView.contentSize.height > scrollView.frame.size.height ?scrollView.contentSize.height / 2 : centerY
      self.imgView?.center = CGPoint(x: centerX, y: centerY)   
  }

5.然后就是使用了,在 didFinishPickingMediaWithInfo 中 跳转到我们的自定义裁剪器即可

//选择图片成功后代理
    func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            picker.dismiss(animated: false, completion: { 
                let clipper = SwiftyPhotoClipper()
                clipper.delegate = self
                clipper.img = image
                self.present(clipper, animated: true, completion: nil)
            })
        } else{
            print("Something went wrong")
        }
    }

完整demo:https://github.com/KFCFans/SwiftyPhotoClipper

求 Star 啦~么么哒~

你可能感兴趣的:(Swift 自定义 UIImagePickerController 选取图片后的裁剪器(相机裁剪界面))