将图像传递给另一个视图控制器(Swift) [英] Passing Image to another View Controller (Swift)

查看:189
本文介绍了将图像传递给另一个视图控制器(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户上传的图像从一个 UIImageView 移动到另一个View Controller上的另一个 UIImageView 。我可以让用户上传图像并将其保存在第一个 UIImageView 中,但在按下发布按钮后,下一个视图控制器上的UIImageView仍为空白。

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.

注意:browsersImage是第二个视图控制器上的UIImageView的名称(目标UIImageView)

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

    }

}


推荐答案

prepareForSegue 中,您无法访问目标视图控制器的@IBOutlet ,因为它们尚未设置。您应该将图像分配给目标视图控制器的属性,然后将其移动到 viewDidLoad 中:

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:

在源视图控制器中:

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

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

在目标视图控制器中:

class ListPage: UIViewController {
    @IBOutlet weak var browsingImage: UIImageView!
    var newImage: UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()
        browsingImage.image = newImage
    }
}

这篇关于将图像传递给另一个视图控制器(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆