Search code examples
iosswiftuiimagepickercontroller

Swift segue not working in ImagePickerController's didFinishPickingMediaWithInfo


I have this code:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        //dismiss(animated: true, completion: nil)
        guard info[UIImagePickerControllerMediaType] != nil else { return }
        let mediaType = info[UIImagePickerControllerMediaType] as! CFString
        print("video is",mediaType)
        switch mediaType {
        case kUTTypeImage:
            if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage, let pickedAsset = info[UIImagePickerControllerPHAsset] as? PHAsset {
                self.screenshotOut = pickedImage
                MyVariables.isScreenshot = true
                self.thumbnailImage = pickedImage
                let creationDate = pickedAsset.creationDate
                self.mediaDate = creationDate
                dismiss(animated: true, completion: nil)
                self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
                }

            }
            break
....
    default:
        dismiss(animated: true, completion: nil)

        print("something else")
        break
    }
}

As you can see its waiting until my imagePickerController has a photo asset. Then it sets some class variables and performs a segue. I have this function to prepare for the segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    if segue.identifier == "CreatePost_Segue" {
        guard self.thumbnailImage != nil else {
            return
        }
        if MyVariables.isScreenshot == true {
            guard self.screenshotOut != nil else {
                return
            }
            //self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
        } else {
            guard self.thumbnailImage != nil else {
                return
            }
            //self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
            //now I set those three varibles?
        }
        let controller = segue.destination as! CreatePostViewController
        controller.postDate = mediaDate
        controller.thumbnailImage = thumbnailImage
        controller.videoURL = videoURL
        controller.screenshotOut = screenshotOut
        print("segued",controller.postDate)
        print("segued",controller.thumbnailImage)
        print("segued",controller.videoURL)
        print("segued",controller.screenshotOut)
    }
}

it prints:

segued Optional(2016-05-04 17:48:53 +0000)
segued Optional(<UIImage: 0x13d365720> size {2448, 3264} orientation 0 scale 1.000000)
segued nil
segued Optional(<UIImage: 0x13d365720> size {2448, 3264} orientation 0 scale 1.000000)

so it seems that all of the controller assignments have worked... the videoURL is intentionally nil and its an optional so I believe thats ok.

But the segue never happens. The picker controller is dismissed which is great but why wouldn't this be segue'ing?


Solution

  • Because dismiss is animated , the VC is currently presenting the camera/photo libaray so it can't present another , you can dispatch it

    dismiss(animated: true, completion: nil)
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5)  {
    
      self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
    
    }
    

    Or for a correct delay

    self.dismiss(animated: true) {
    
       self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
    
    }
    

    // you may look for a message like VC is currently presenting // in console