UIImagePickerController editing view circle overlay

后端 未结 5 1248
太阳男子
太阳男子 2020-12-13 02:58

I\'ve been able to get pretty far with what I\'ve been wanting to accomplish, and that\'s to replicate iOS\'s built in circular photo cropper for the built in contacts app.

5条回答
  •  不思量自难忘°
    2020-12-13 03:23

    In the code of Jakub Marek, there's an issue with persistent rounded layer if you open a second time the camera.

    to solve it add in your openCamera func:

    editLayer?.removeFromSuperlayer()
    label?.removeFromSuperview()
    

    and replace in private func hideDefaultEditOverlay(view: UIView)

    subview.isHidden = true
    

    by

    subview.removeFromSuperview()
    

    Code :

    func openCamera(){
        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera)){
            imagePicker.sourceType = UIImagePickerController.SourceType.camera
            //If you dont want to edit the photo then you can set allowsEditing to false
            imagePicker.allowsEditing = true
            imagePicker.cameraDevice = .rear
            imagePicker.showsCameraControls = true
            imagePicker.cameraCaptureMode = .photo
    
            imagePicker.delegate = self
            editLayer?.removeFromSuperlayer()
            label?.removeFromSuperview()
            self.present(imagePicker, animated: true, completion: nil)
    
        }
        else{
            let alert  = UIAlertController(title: NSLocalizedString("Attention",comment:""), message: NSLocalizedString("You don't have any camera",comment:""), preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: NSLocalizedString("OK",comment:""), style: .default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    private func hideDefaultEditOverlay(view: UIView)
    {
        for subview in view.subviews
        {
            if let cropOverlay = NSClassFromString("PLCropOverlayCropView")
            {
                if subview.isKind(of: cropOverlay) {
                    subview.removeFromSuperview()
                    //subview.isHidden = true
                    break
                }
                else {
                    hideDefaultEditOverlay(view: subview)
                }
            }
        }
    }
    

提交回复
热议问题