1

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?

Burkhard
  • 14,596
  • 22
  • 87
  • 108
dlao
  • 351
  • 2
  • 9
  • Could you post the code with the IBAction please? – Akaino Aug 29 '14 at 10:41
  • @Akaino, it's exactly the same code, but **initPicker()** into **viewDidLoad()** desappears. Also, **initPicker()** function definition becomes **@IBAction func initPicker(){...}** – dlao Aug 29 '14 at 14:39

2 Answers2

0

Try declaring the object of UIImagePickerController as an Instance level parameter instead of within the function.

Gandalf
  • 2,399
  • 2
  • 15
  • 19
0

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?

dlao
  • 351
  • 2
  • 9