Passing Image to another View Controller (Swift)

2019-01-06 21:34发布

问题:

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)

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

    }

}

回答1:

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


回答2:

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



回答3:

IBOutlets are not accessible in your current view controller. The outlets are not yet initialized since the your second view is not yet loaded.

Instead you can create a UIImage variable in your second viewController and assign the value to it in the prepareForSegue.

And in your second viewController's viewDidLoad method assign that value to your ImageView outlet.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "toBrowsePage" {
        let destination = segue.destinationViewController as! ListPage
        destination.receivedImage = postingImage.image
    }
}

In your next viewController declare a variable called

@IBOutlet weak var postingImage: UIImageView!
var receivedImage : UIImage?

In the viewDidLoad method

postingImage.image = receivedImage


回答4:

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