skip to Main Content

I am trying to add a image upload feature to my iOS app. I have a function selectPicture() that gets called when user clicks on the upload button which then pops up an alert that allows the user to choose between camera and library.

func selectPicture() {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    let alertController = UIAlertController(title: "Image Source", message: "Please choose your image source", preferredStyle: UIAlertControllerStyle.alert)
    let camera_action = UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) {
        (_: UIAlertAction) -> Void in
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            print ("Camera Selected")
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera
            imagePicker.allowsEditing = true
            imagePicker.modalTransitionStyle = UIModalTransitionStyle.coverVertical
            self.present(imagePicker, animated: true, completion: nil)
        }
    }
    let photoLibrary_action = UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.default) {
        (_: UIAlertAction) -> Void in
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
            print ("Photo Library Selected")
            
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
            imagePicker.allowsEditing = true
            imagePicker.navigationBar.titleTextAttributes = self.navbarAttributes
            imagePicker.navigationBar.tintColor = UIColor.white
            imagePicker.navigationBar.isTranslucent = false
            imagePicker.navigationBar.barTintColor = PING_ORANGE
            imagePicker.navigationController?.title = "Pick Image"
            imagePicker.modalTransitionStyle = UIModalTransitionStyle.coverVertical
            self.present(imagePicker, animated: true, completion: nil)
        }
    }
    let cancel_action = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
        (_: UIAlertAction) -> Void in
        print ("Cancel Selected")
    }
    alertController.addAction(camera_action)
    alertController.addAction(photoLibrary_action)
    alertController.addAction(cancel_action)
    self.present(alertController, animated: true, completion: nil)
    
}

Everything works fine till this point but when I select an image the delegate method is not being called.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any], editingInfo: [AnyHashable: Any]!) {
    let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    print("Image Print")
    print(image)
    if image != nil {
        print ("INSIDE IMAGE DID FINISH PICKING")
        dismiss(animated: false) {
            self.uploadProfileImage(image: image!)
        }
        
    } else {
        dismiss(animated: false) {
            
        }
    }
}

I’m new to swift so I might be missing something. Can anyone help me with this?

Thank you.

3

Answers


  1. Chosen as BEST ANSWER

    I fixed this by using:

    func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            dismiss(animated: false) {
                self.uploadProfileImage(image: image)
       
            }
        }
        else {
            dismiss(animated: false) {
            }
        }
    }
    

  2. You are probably using an old code. Your method signature is wrong. It should be:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[.originalImage] as? UIImage {
            print("Image Print")
            print(image)
            dismiss(animated: false) {
                self.uploadProfileImage(image: image)
            }
        } else {
            dismiss(animated: false)
        }
    }
    
    Login or Signup to reply.
  3. You need to move let imagePicker = UIImagePickerController() to outside the scope of the function to prevent it from getting deallocated

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search