Search code examples
iosswiftuiimagepickercontrollerdeallocxcode6-beta6

app crashes after presenting an UIImagePickerController


I'm trying to present an UIImagePickerController in order to get an image from album and I'm facing a weird behavior.

If I launch the image picker in the viewDidLoad, it works:

class CaptureImageViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    initPicker()
}

func initPicker() {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    presentViewController(picker, animated: true, completion: nil)
}

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

func imagePickerControllerDidCancel(picker: UIImagePickerController!) {
    dismissViewControllerAnimated(true, completion: nil)
}

}

However, if I turn the initPicker() into a @IBAction and call it from a Button, after tapping it, the image picker appears but suddenly the app crashes with this error:

CaptureImageViewController respondsToSelector:]: message sent to deallocated instance 0x7960b1e0

I'm working on simulator with XCode Beta 6

Any idea?


Solution

  • After testing a bunch of different ways with no success, I've changed the function signature and... it works!

    It seems the init prefix confuses Swift.

    I'm looking for some related topic in forums and official documentation, but I'm not getting anything.

    Maybe a bug?