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)
}
}