-1

I am writing a basic photo app that allows a user to push a "+" button in a navigation controller and then an ActionSheet pops up from the bottom giving the user options, here is a picture:

Screenshot of my app

I have the code to get this app working using normal buttons, but when I added it to my code for this app it crashes. Here is the code:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{

    @IBOutlet weak var imageView: UIImageView!

    @IBAction func addPhoto(sender: UIBarButtonItem)
    {
        let photoOption = UIAlertController(title: nil, message: "Select an Input Type", preferredStyle: .ActionSheet)

        let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .Default) { (alert: UIAlertAction!) -> Void in
            println("Photo Library Selected")   // Used for debugging

            // This code worked in my previous app
            let picker = UIImagePickerController()

            picker.delegate = self
            picker.sourceType = .PhotoLibrary

            self.presentViewController(picker, animated: true, completion: nil)
        }

        let cameraAction = UIAlertAction(title: "Camera", style: .Default) { (alert: UIAlertAction!) -> Void in
            println("Camera Selected")  // Used for debugging

            // This code worked in my previous app
            let picker = UIImagePickerController()

            picker.delegate = self
            picker.sourceType = .Camera

            self.presentViewController(picker, animated: true, completion: nil)
        }

        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

        photoOption.addAction(photoLibraryAction)
        photoOption.addAction(cameraAction)
        photoOption.addAction(cancelAction)

        self.presentViewController(photoOption, animated: true, completion: nil)
    }

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

}

and the strange thing is after I ran this and the app crashed, I reverted back to when the code was working and the app still crashed.

steveclark
  • 537
  • 9
  • 27
  • 4
    What is the crash? You can't expect people to guess here or do your work for you. – Léo Natan Apr 10 '15 at 18:12
  • 1
    @LeoNatan Obviously he _can_ expect it. It's just that he expects wrong. :) – matt Apr 10 '15 at 18:18
  • 1
    Unfortunately I no longer have the error. Since that app no longer worked I had to start over and I don't want to purposely crash my code again. When I ran the app, the build was successful, the simulator opened and then immediately crashed. It then took me to the app delegate where `class AppDelegate: UIResponder, UIApplicationDelegate` was highlighted in green and had some message. I know it's not that helpful, but it might sound familiar to someone. – steveclark Apr 10 '15 at 18:23

1 Answers1

1

The problem is that you are running this code in the simulator. But the simulator has no camera. It's just a simulator!

Hence, trying to summon an image picker controller with a .Camera source type causes an exception - with a helpful error message in the console that you could have read, thus avoiding wasting bandwidth with this question...

matt
  • 515,959
  • 87
  • 875
  • 1,141