将图像从ViewController传递到ViewController

时间:2018-05-23 14:00:28

标签: ios iphone swift xcode

点击按钮时,我尝试将图像从一个ViewController1传递到另一个ViewController2。我在故事板中使用segues

ViewController1

import UIKit

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    var imagePicker: UIImagePickerController!

    @IBAction func editPhoto(_ sender: UIButton) {
        let editViewController = storyboard?.instantiateViewController(withIdentifier: "EditImageViewController") as! EditImageViewController
        editViewController.imageForEdit = imageView.image!
        print(imageView)
        print(imageView.image!)
        navigationController?.pushViewController(editViewController, animated: true)
    }

    @IBOutlet weak var imageView: UIImageView! {
        didSet {
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

print(imageView)会产生

  

some(UIImageView:0x7fc34df0e630; frame =(8 29; 303 443); clipsToBounds = YES; opaque = NO; autoresize = RM + BM; userInteractionEnabled = NO; layer = CALayer:0x60000023e520)

和<{p>中的print(imageView.image!)

  

size {3000,2002} orientation 0 scale 1.000000

ViewController2

import UIKit

class EditImageViewController: UIViewController {

    var imageForEdit = UIImage()

    @IBOutlet weak var editingImage: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        editingImage.image = imageForEdit
        print(imageForEdit)
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

print(imageForEdit)会产生

  

尺寸{0,0}方向0比例1.000000

为什么?为什么图像没有传递给ViewController2

4 个答案:

答案 0 :(得分:1)

在我看来,这可能会简单得多。如果您使用带有segue的Storyboard,则可以在prepare(for segue: UIStoryboardSegue, sender: Any?)中轻松完成此操作。这是一个示例故事板:

storyboard

在这种情况下,ViewController在Edit Image的{​​{1}}按钮上有一个segue。

当您按下该按钮时,您会收到EditImageViewController电话。这是一个示例ViewController:

prepare(for segue:

这是class ViewController: UIViewController { var imagePath = Bundle.main.path(forResource: "icon8", ofType: "png") var anEditableImage:UIImage? override func viewDidLoad() { super.viewDidLoad() if let imagePath = imagePath { anEditableImage = UIImage(contentsOfFile: imagePath) } } // MARK: - Navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let destination = segue.destination as? EditImageViewController { destination.imageForEdit = anEditableImage } } }

EditImageViewController

使用class EditImageViewController: UIViewController { var imageForEdit:UIImage? override func viewDidLoad() { super.viewDidLoad() print(imageForEdit ?? "Image was nil") } } 是基于Storyboard的应用在视图控制器之间传递数据的首选方式。这很简单,也很有效。

如果您想查看工作代码,示例项目就在这里:

https://www.dropbox.com/s/eog01bmha67c5mv/PushImage.zip?dl=0

答案 1 :(得分:0)

var imageForEdit = UIImage()替换为var imageForEdit : UIImage?

我自己测试代码,确保imageView有图像

 @IBOutlet weak var imageView: UIImageView! {
        didSet {
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
        }
    }

答案 2 :(得分:0)

var imageForEdit: UIImage? {
    didSet {
        editingImage.image = imageForEdit
    }
}

答案 3 :(得分:0)

您可以创建一个公共变量,以便将数据从一个视图控制器缓存到另一个视图控制器。

class variables
{
    static var image = UIImage()
}

2控制器:

@IBOutlet weak var anotherviewcontroller: UIImageView!

anotherviewcontroller.image = variables.image