Setting delegate for UIImagePicker returns error

Jacob King picture Jacob King · Mar 13, 2015 · Viewed 15.1k times · Source

Having a problem in some swift code I had written for an OCR translation app. The code snippet is below:

@IBAction func btnOCR(sender: AnyObject) {

    var languageAlert = UIAlertController(title: "For Your Information...", message: "The OCR feature currently only supports English & French.", preferredStyle: .Alert)
    languageAlert.addAction(UIAlertAction(title: "Okay", style: .Default, handler: { action in

        var image = UIImagePickerController()
        image.sourceType = UIImagePickerControllerSourceType.Camera
        image.allowsEditing = false
        image.delegate = self
        presentViewController(image, animated: true, completion: nil)

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

The image.delegate = self line returns the error: Cannot assign a value of type viewcontroller to uiimagepickerdelegate.

I have set the delegate in the class definition, this can be seen below...

class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UIImagePickerControllerDelegate {    }

All and any help would be appreciated, thanks in advance.

Answer

Pavel Gatilov picture Pavel Gatilov · Mar 13, 2015

You forgot about UINavigationControllerDelegate in your ViewController class defenition.

The image picker’s delegate object.

Declaration

unowned(unsafe) var delegate: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>?