user3662992
user3662992

Reputation: 688

Issue with Segue image to another viewcontroller

I've 2 VC :

The first VC with 1 button when tapped >> 2 options post image form cam or library

Second VC chosen image form VC1 segued to the second VC

I tried to figured This the error without any success

fatal error: unexpectedly found nil while unwrapping an Optional value pointed at

  imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage

I uploaded the project in dropbox https://www.dropbox.com/s/w7blbnyac9qyxgw/segImage.zip?dl=0

VC1

    class ViewController:UIViewController,UIAlertViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate
{
    @IBOutlet weak var btnClickMe: UIButton!
    @IBOutlet weak var imageView: UIImageView!
    var picker:UIImagePickerController?=UIImagePickerController()
    var popover:UIPopoverController?=nil

    override func viewDidLoad()
    {
        super.viewDidLoad()
        picker!.delegate=self
        // 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.
    }



    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "" ) {
            let destVC : second = segue.destinationViewController as! second
            destVC.pics = imageView.image!
        }
    }

    @IBAction func btnImagePickerClicked(sender: AnyObject)
    {
        let alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

        let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default)
            {
                UIAlertAction in
                self.openCamera()

        }
        let gallaryAction = UIAlertAction(title: "Gallary", style: UIAlertActionStyle.Default)
            {
                UIAlertAction in
                self.openGallary()
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
            {
                UIAlertAction in

        }

        // Add the actions
        picker?.delegate = self
        alert.addAction(cameraAction)
        alert.addAction(gallaryAction)
        alert.addAction(cancelAction)
        // Present the controller
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone
        {
            self.presentViewController(alert, animated: true, completion: nil)
        }
        else
        {
            popover=UIPopoverController(contentViewController: alert)
            popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
        }
    }
    func openCamera()
    {
        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
        {
            picker!.sourceType = UIImagePickerControllerSourceType.Camera
            self .presentViewController(picker!, animated: true, completion: nil)
        }
        else
        {
            openGallary()
        }
    }
    func openGallary()
    {
        picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone
        {
            self.presentViewController(picker!, animated: true, completion: nil)
        }
        else
        {
            popover=UIPopoverController(contentViewController: picker!)
            popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
        }
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        picker .dismissViewControllerAnimated(true, completion: nil)
        imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController)
    {
        print("picker cancel.")
    }


}

second VC

class second: UIViewController {

var pics = UIImage()

@IBOutlet var image: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()

    if let img = UIImage(named: "pics")
    {
        self.image.image = img
    }
}

Upvotes: 0

Views: 48

Answers (1)

Andy Obusek
Andy Obusek

Reputation: 12842

Your storyboard wiring is wrong.

The image should really be set on an instance of second rather than an instance of ViewController. Notice that the crash is happening in ViewController and not second

This code:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
    picker .dismissViewControllerAnimated(true, completion: nil)
    imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
}

is attempting to set an imageView.image in ViewController. If you look at the Storyboard, there is no UIImageView in ViewController.

enter image description here

The way that you've defined imageView on ViewController is what makes this crash:

@IBOutlet weak var imageView: UIImageView!

With the ! there, you are saying, "This will never be nil." Well, that's obviously not the case because since this IBOutlet isn't wired to a UIImageView then it is nil. And thus, when you try to access it, BOOM!

To fix this:

  • Segue to second when the user picks an image
  • Set the selected image on second's image.imageView property

Other suggestion: - Use consistency in naming, eg second vs SecondViewController

Upvotes: 1

Related Questions