Reputation: 25
I am trying to pass the image which is captured from UIImagePickerController
to another ViewController
but the image is not getting passed.
This is the code of the first ViewController
:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
newImageView.contentMode = .scaleAspectFit
newImageView.image = chosenImage
self.performSegue(withIdentifier: "pass", sender: self)
dismiss(animated:true, completion: nil)
let svc = self.storyboard!.instantiateViewController(withIdentifier: "demoViewController") as! demoViewController
present(svc, animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "pass"
{
let imgpass = segue.destination as! demoViewController
imgpass.newphoto = chosenImage
}
}
Code for the second ViewController
:
override func viewDidLoad() {
super.viewDidLoad()
myImageView.image = newphoto
}
I went through a lot of other similar postings and implemented them too but couldn't get the image passed.
Upvotes: 0
Views: 87
Reputation:
Try this
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
if let chosenImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
newImageView.contentMode = .scaleAspectFit
newImageView.image = chosenImage
picker.dismiss(animated: true, completion: nil)
let svc = self.storyboard!.instantiateViewController(withIdentifier: "demoViewController") as! demoViewController
svc.newphoto = chosenImage
self.present(svc, animated: true, completion: nil)
}
}
Upvotes: 1
Reputation: 535231
The problem is that your func prepare(for:sender:)
implementation is never called, so your newphoto
is never set. prepare(for:sender:)
is sent before a segue. But there is no segue. You are presenting in code, not with a segue.
Upvotes: 1