-1

我正在编写一个基本的照片应用程序,它允许用户在导航控制器中按下“+”按钮,然后从底部弹出一个 ActionSheet,为用户提供选项,这是一张图片:

我的应用程序的屏幕截图

我有使用普通按钮让这个应用程序工作的代码,但是当我将它添加到我的这个应用程序的代码中时它崩溃了。这是代码:

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)
    }

}

奇怪的是,在我运行它并且应用程序崩溃之后,我又回到了代码正常工作并且应用程序仍然崩溃的时候。

4

1 回答 1

1

问题是您正在模拟器中运行此代码。但是模拟器没有摄像头。这只是一个模拟器!

因此,尝试调用具有.Camera源类型的图像选择器控制器会导致异常 - 您可以在控制台中阅读有用的错误消息,从而避免在这个问题上浪费带宽......

于 2015-04-10T18:20:35.260 回答