Search code examples
swiftuiviewcontrollerphoto

Swift ImagePicker - View controller


Whats up,

So this is my code to pick an image from the photo library, now I want to go send the user to the next view controller but only after the user picks an image. my question is:

How do I make it go to the next view controller but only after an image has been selected from the photo library?

By either making it so the finish button only shows up after the user has selected an image or by just showing error if they try to click finished until an image has been selected from the library

Thank you!!!!


override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.            
        
              let imageTap = UITapGestureRecognizer(target: self, action: #selector(openImagePicker))
                       ProfilePictureImageView.isUserInteractionEnabled = true
                       ProfilePictureImageView.addGestureRecognizer(imageTap)
                       ProfilePictureImageView.layer.cornerRadius = ProfilePictureImageView.bounds.height / 2
                       ProfilePictureImageView.clipsToBounds = true
                       TapToChangeButton.addTarget(self, action: #selector(openImagePicker), for: .touchUpInside)
                       
                       //when user clicks they can choose a photo from library
                       //instantiate image picker
                       ImagePicker = UIImagePickerController()
                       ImagePicker.allowsEditing = true
                       ImagePicker.sourceType = .photoLibrary
                       ImagePicker.delegate = self
                       
                   
                     setUpElements()
                    }
                     
                    func setUpElements() {
    
                    ErrorLabel.alpha = 0
                    }
    
                   // taping to change and add a photo
                   @objc func openImagePicker(_ sender:Any) {
                       // Open Image Picker
                       self.present(ImagePicker, animated: true, completion: nil)
                   }
                
                   @IBAction func FinishTapped(_ sender: Any) {
                   }
                   
                   
               }
               //extend the proper delagate method
               extension BackroundCheckViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
                   //cancel
                   func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
                       picker.dismiss(animated: true, completion: nil)
                       
                   }
                   //pick an image
                   func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
                       
                       //get the image the selected
                       if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
                           
                           self.ProfilePictureImageView.image = pickedImage
                        
                        //upload to firbase
                        PhotoService.savePhoto(image: pickedImage)
                        
                       }
                       picker.dismiss(animated: true, completion: nil)
                                           
                       }
                                
                     }

Solution

  • Add the code to navigate to another view controller in the completion block of the picker dismissal in the didFinishPickingMediaWithInfo method of UIImagePickerControllerDelegate.

    extension BackroundCheckViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        //pick an image
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            //...
            picker.dismiss(animated: true) {
                // add you code here to navigate to somewhere else.
                let myViewController = MyViewController()
                if let navigationController = self.navigationController {
                    navigationController.pushViewController(myViewController, animated: true)
                } else {
                    self.present(myViewController, animated: true)
                }
            }
        }
    }