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

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

问题描述

我正在尝试将用户上传的图像从一个 UIImageView 移动到另一个视图控制器上的另一个 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.

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

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

任何帮助将不胜感激!

@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

    }

}

推荐答案

prepare(for:) 中你不能访问目标视图的 @IBOutlets控制器,因为它们尚未设置.您应该将图像分配给目标视图控制器的属性,然后将其移动到 viewDidLoad() 中:

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():

在源视图控制器中:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toBrowsePage" {
        let dvc = segue.destination 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天全站免登陆