Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

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

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

    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In prepare(for:) 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 prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toBrowsePage" {
        let dvc = segue.destination 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
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...