0

In an app I am working on I have an image picker that is called by tapping a button on view controller 1. After I select an image I want to send it to view controller 2 so that a user can enter metadata about the image before uploading.

In the code below I have this working fine, but there is one thing that I can't figure out. When I segue to view controller 2 the first view controller always flashes onto the screen instead of immediately segueing to view controller 2. The series of events appears to be image picker dismisses -> view controller 1 appears briefly -> prepare to segue processes -> then view controller 2 loads.

Is there a way I can change my code so that the segue to view controller 2 occurs immediately after the image is selected? If an image is not selected because the image picker is cancelled I would want to go back to view controller 1.

import UIKit
import Parse

class UserHome: UIViewController {

var newImage: UIImage?

@IBOutlet weak var welcomeLabel: UILabel!
@IBOutlet weak var imageArea: UIImageView!
@IBOutlet weak var currentUser: UIButton!
@IBAction func tapLogOut(_ sender: Any) {
    PFUser.logOut()
}
@IBAction func tapWeddingInfo(_ sender: Any) {
    opeURLFromString(urlString: weddingURL)
}
@IBAction func tapSlideShow(_ sender: Any) {
    self.performSegue(withIdentifier: "showSlideShow", sender: self)
}
@IBAction func tapAddImage(_ sender: Any) {
    print("tapped add image")
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .photoLibrary
    imagePicker.allowsEditing = false
    self.present(imagePicker, animated: true, completion: nil)
}
override func viewDidLoad() {
    super.viewDidLoad()
    loadPage()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showImageSubmit"{
        let vc = segue.destination as! ImageSubmit
        vc.newImage = newImage
    }
}
func loadPage() {
    print("query a database for images")
    print("load an array with those images")
    // Figure out a way to have images scroll into viewing area
    if let userEmail = PFUser.current()?.username {
        currentUser.setTitle(userEmail, for: [])
    }
    imageArea.image = UIImage(named: userHomeImage)
    welcomeLabel.text = userHomeMsg

    print ("loaded User Home")
    }
}
extension UserHome: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            // Convert image into PFFile data type
            // self.imageArea.image = image
            newImage = image
            self.dismiss(animated: true, completion: nil)
        }
        self.performSegue(withIdentifier: "showImageSubmit", sender: self)
    }

}
Daniel Patriarca
  • 361
  • 3
  • 20
  • try this `self.dismiss(animated: false, completion: { self.performSegue(withIdentifier: "showImageSubmit", sender: self) })` – Anbu.Karthik Apr 01 '19 at 05:58
  • I found this, but wanted the image in view controller 1, so I wouldn't know how to pass the image back to it. https://stackoverflow.com/questions/31374076/swift-segue-directly-to-a-view-controller-from-camera-uiimagepickercontroller – Daniel Patriarca Apr 01 '19 at 06:00

1 Answers1

1

You can use completion of dismiss method. After UIImagePickerController is dismissed, then next controller appears

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    guard let image = info[.originalImage] as? UIImage else { 
        // dismiss(animated: true)
        return
    }
    // Convert image into PFFile data type
    // self.imageArea.image = image
    newImage = image

    dismiss(animated: true) {
        self.performSegue(withIdentifier: "showImageSubmit", sender: self)
    }
}
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
  • use `false` instead of `true` it omit the animation – Anbu.Karthik Apr 01 '19 at 06:10
  • @Anbu.Karthik okay. It depends. – Robert Dresler Apr 01 '19 at 06:15
  • I made that change and set animate to false on the segue and dismiss and it does a weird flash now. I appreciate the help but think there must be another way to fully control how the image picker redirects? – Daniel Patriarca Apr 01 '19 at 06:17
  • @DanielPatriarca maybe you should rethink your app. If you need data from image picker controller to first vc, why do you need to present second vc? Also you play around with presenting animations of controllers. – Robert Dresler Apr 01 '19 at 06:20
  • Flow is VC1 has image view where swiping left right shows images. It has a button for adding a new image. When you add a new image I want to capture metadata like a caption. My approach was button tap on VC1 opens image picker... when you select image I pass it with prepare to segue to VC2 with a text box to enter the caption and SAVE button. Save button tap saves to server and returns to VC1 with the new image added to the images array with metadata. If they cancel from VC2 no save to server occurs and no add to array occurs. I am open to different approaches, any suggestions? – Daniel Patriarca Apr 01 '19 at 21:38