Passing Image to another View Controller (Swift)

I’m trying to move a user uploaded image from one UIImageView to another UIImageView on a different View Controller. I’m able to have the user upload an image and have it saved in the first UIImageView but after they press the “Post” button, the UIImageView on the next view controller remains blank.

Note: browsingImage is the name of the UIImageView on the second view controller (destination UIImageView)

  • How to change iOS status bar color in child view controller
  • I need to refresh the content of a view as soon as I popViewController
  • Setting property value of parent viewcontroller class from child viewcontroller?
  • iOS 7 custom transition glitch
  • dismissViewControllerAnimated VS popViewControllerAnimated
  • Reloading a ViewController
  • Any help would be greatly appreciated!

    @IBAction func cameraButton(sender: AnyObject) {
    
        addNewPicture()
    
    }
    
    
    func addNewPicture() {
    
        let picker = UIImagePickerController()
        picker.allowsEditing = true
        picker.delegate = self
    
        presentViewController(picker, animated: true, completion: nil)
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    
        postingImage.image = image
        self.dismissViewControllerAnimated(true, completion: nil)
    
    }
    
    @IBAction func postButton(sender: AnyObject) {
    
        performSegueWithIdentifier("toBrowsePage", sender: nil)
    
    
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
        if segue.identifier == "toBrowsePage" {
    
    
            var itemToAdd = segue.destinationViewController as! ListPage
    
            itemToAdd.postingImage.image = browsingImage.image
    
        }
    
    }
    

    3 Solutions Collect From Internet About “Passing Image to another View Controller (Swift)”

    In prepareForSegue you can’t access the @IBOutlets of the destination view controller because they haven’t been set up yet. You should assign the image to a property of the destination view controller, and then move it into place in viewDidLoad:

    In source view controller:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
        if segue.identifier == "toBrowsePage" {
            let dvc = segue.destinationViewController as! ListPage
            dvc.newImage = postingImage.image
        }
    }
    

    In destination view controller:

    class ListPage: UIViewController {
        @IBOutlet weak var browsingImage: UIImageView!
        var newImage: UIImage!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            browsingImage.image = newImage
        }
    }
    

    From your description,browsingImage is in destination viewController,so
    in this line

    itemToAdd.postingImage.image = browsingImage.image
    

    You pass the destination imageview to source imageview

    At the time of the Segue happening your UIImageView is not initialised so you can not assign an image to it, the best practise would be to pass the UIImage and initialise your UIImageView in viewDidLoad.

    so in your ListPage class make a property of type UIImage change your prepareForSegue line as

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
        if segue.identifier == "toBrowsePage" {
    
    
            var itemToAdd = segue.destinationViewController as! ListPage
    
            itemToAdd.image = browsingImage.image
    
            }
    
        }
    

    in viewDidLoad or viewDidAppear of your destination viewController you will do something like this

    browsingImage.image = image