简体   繁体   中英

Transfer Image View from one View Controller to another in Swift

I've been fumbling around with a camera application this afternoon in Xcode and I've come upon a road block. I'm looking to transfer the image view from the Camera to the ViewController using a prepareForSegue method. With the rest of my code in the Camera class, I can successfully display an image to the image view on my storyboard, so that's not the problem. The problem is displaying it to the image view in the main ViewController. I have everything hooked up correctly in the Storyboard. Thanks for your help!

First View Controller:

class ViewController: UIViewController {

@IBOutlet weak var photoViewData: UIImageView!

} 

Second View Controller:

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
}

I think it will be better to pass UIImage between the controllers.

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
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM