iOS Custom UIImagePickerController Camera Crop to circle - in preview view

后端 未结 4 660
花落未央
花落未央 2020-12-20 08:45

I\'m using this code to make a custom camera crop:

UIImagePickerController editing view circle overlay

This works perfectly in camera roll but not taking pho

4条回答
  •  星月不相逢
    2020-12-20 09:16

    extension ProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        guard imagePickerController?.sourceType == .camera else {
            return
        }
    
        guard let view = viewController.view.subviews(deep: true, where: {
            String(describing: type(of:$0)) == "CAMPreviewView"
        }).first else {
            return
        }
    
        viewController.view.layoutIfNeeded()
    
        let camPreviewBounds = view.bounds
        let circleRect = CGRect(
            x: camPreviewBounds.minX + (camPreviewBounds.width - 320) * 0.5,
            y: camPreviewBounds.minY + (camPreviewBounds.height - 320) * 0.5,
            width: 320,
            height: 320
        )
    
        let path = UIBezierPath(roundedRect: camPreviewBounds, cornerRadius: 0)
        path.append(UIBezierPath(ovalIn: circleRect))
    
        let layer = CAShapeLayer()
        layer.path = path.cgPath
        layer.fillRule = CAShapeLayerFillRule.evenOdd;
        layer.fillColor = UIColor.black.cgColor
        layer.opacity = 0.8;
    
        view.layer.addSublayer(layer)
    }
    }
    

提交回复
热议问题