代码之家  ›  专栏  ›  技术社区  ›  four-eyes

将图像从ViewController传递到ViewController

  •  1
  • four-eyes  · 技术社区  · 6 年前

    我试图传递一个图像 ViewController1 到另一个 ViewController2 当一个按钮被点击时。我正在使用 segues 在我的故事板里。

    视图控制器1

    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) 结果

    一些(uiImageView:0x7fc34df0e630;frame=(8 29;303 443);clipsToBounds=yes;opaque=no;autoresize=rm+bm;userInteractionEnabled=no;layer=calayer:0x60000023e520)

    print(imageView.image!) 在里面

    尺寸3000,2002方向0比例1.000000

    视图控制器2

    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

    为什么?为什么不把图像传递给 视图控制器2 ?

    4 回复  |  直到 6 年前
        1
  •  1
  •   David S.    6 年前

    在我看来,这可能要简单得多。如果你用一个故事板和一个塞格,你可以很容易地完成这一点在 prepare(for segue: UIStoryboardSegue, sender: Any?) . 下面是一个故事板示例:

    storyboard

    在这种情况下,ViewController在 Edit Image 按钮到 EditImageViewController .

    当你按下那个按钮,你就会 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
            }
         }
    }
    

    这是 编辑图像查看控制器 :

    class EditImageViewController: UIViewController {
    
        var imageForEdit:UIImage?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            print(imageForEdit ?? "Image was nil")
        }
    }
    

    使用 准备(对于segue:uiStoryboardsegue,sender:any?) 是基于情节提要的应用程序在视图控制器之间传递数据的首选方法。这很简单,而且很有效。

    如果要查看工作代码,请在此处显示示例项目:

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

        2
  •  0
  •   Alexey Kudlay    6 年前
    var imageForEdit: UIImage? {
        didSet {
            editingImage.image = imageForEdit
        }
    }
    
        3
  •  0
  •   Abdelahad Darwish    6 年前

    更换 var imageForEdit = UIImage() 具有 var imageForEdit : UIImage?

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

     @IBOutlet weak var imageView: UIImageView! {
            didSet {
                imageView.contentMode = .scaleAspectFill
                imageView.clipsToBounds = true
            }
        }
    
        4
  •  0
  •   Frederik L. Frandsen    6 年前

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

    class variables
    {
        static var image = UIImage()
    }
    

    2号控制器:

    @IBOutlet weak var anotherviewcontroller: UIImageView!
    
    anotherviewcontroller.image = variables.image