Thread 1: signal SIGABRT error when using IBAction

2019-03-03 17:47发布

问题:

I am trying to create a project using this tutorial. I did the best I could trying to recreate the code but am having some problems. Here is my code:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet var ImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func choosePhoto(sender: AnyObject) {

    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    let actionSheet = UIAlertController(title: "Choose image source", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

    actionSheet.addAction(UIAlertAction(title: "Camera roll", style: UIAlertActionStyle.Default, handler: { ( alert: UIAlertAction!) -> Void in
        imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
        self.presentViewController(imagePickerController, animated: true, completion: nil)
    } ))

    actionSheet.addAction(UIAlertAction(title: "Take Photo", style: UIAlertActionStyle.Default, handler: { ( alert: UIAlertAction!) -> Void in
        imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
        self.presentViewController(imagePickerController, animated: true, completion: nil)
    } ))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

    self.presentViewController(actionSheet, animated: true, completion: nil)

}

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

@IBAction func applyFilter(sender: AnyObject) {

    applyFilterWithName("CIPhotoEffectMono")


}

func applyFilterWithName(filterName: String){
    let originlOrientation = ImageView.image!.imageOrientation
    let originalScale = ImageView.image!.scale

    let inputImage = CIImage(image : ImageView.image)
    let drawingContext = CIContext(options: nil)

    let filterParameters = [kCIInputImageKey: inputImage]

    let filter = CIFilter(name: filterName, withInputParameters: filterParameters)

    let outputImage = filter.outputImage

    let imageRef = drawingContext.createCGImage(outputImage, fromRect: outputImage.extent())

    ImageView.image = UIImage(CGImage: imageRef, scale: originalScale, orientation: originlOrientation)


}

@IBAction func saveImage(sender: AnyObject) {

    UIImageWriteToSavedPhotosAlbum(ImageView.image, nil, nil, nil)

}

I know this is the main part of the debugger output I have to look at:

Image Filter[20320:1554357] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type 1 not available'

I can't seem to figure out where this error is or how to fix it. Once again, I did the best I could to recreate the code but there could be errors.

Any suggestions or input would be greatly appreciated.

Thanks in advance.

回答1:

Your code is senseless. You are saying:

actionSheet.addAction(UIAlertAction(title: "Camera roll", style: UIAlertActionStyle.Default, handler: { ( alert: UIAlertAction!) -> Void in
    imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
    self.presentViewController(imagePickerController, animated: true, completion: nil)
} ))

If the user is going to tap a "Camera roll" button, why would you then create an image picker controller with a Camera source type? That isn't what the user asked for.

The error, by the way, simply means there is no camera. You are not trying to run this code in the Simulator, are you? That would be senseless too. The camera exists only on a device — a device with a camera.