在Swift中将图像视图从一个View Controller传输到另一个View Controller

时间:2015-03-21 19:00:15

标签: ios image swift uiimageview segue

我今天下午在Xcode中使用相机应用程序一直在摸索,我已经遇到了路障。我正在寻找使用prepareForSegue方法将图像视图从Camera传输到ViewController。使用Camera类中的其余代码,我可以成功地将图像显示在故事板上的图像视图中,这样就不会出现问题。问题是将其显示在主ViewController中的图像视图中。我在故事板中正确连接了所有内容。谢谢你的帮助!

First View Controller:

class ViewController: UIViewController {

@IBOutlet weak var photoViewData: UIImageView!

} 

第二视图控制器:

class Camera: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet var imageView:UIImageView!
@IBOutlet var takePictureButton:UIButton!
var moviePlayerController: MPMoviePlayerController?
var image:UIImage?
var movieURL: NSURL?
var lastChosenMediaType: String?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var destViewController: ViewController = segue.destinationViewController as ViewController

    destViewController.photoViewData = imageView
}

1 个答案:

答案 0 :(得分:0)

我认为在控制器之间传递UIImage会更好。

class ViewController: UIViewController 
{

 strong var photo: UIImage!

 override function viewDidLoad()
 {
   if(self.photo)
   self.imageView.image = photo;
 }
} 

class Camera: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate 
{

   @IBOutlet var imageView:UIImageView!
   @IBOutlet var takePictureButton:UIButton!
   var moviePlayerController: MPMoviePlayerController?
   var image:UIImage?
   var movieURL: NSURL?
   var lastChosenMediaType: String?

   override func prepareForSegue(segue: UIStoryboardSegue, sender:AnyObject?)
   {
      var destViewController: ViewController = segue.destinationViewController as ViewController

      destViewController.photo = imageView.image
   }