2014-08-29 133 views
1

我試圖提出一個UIImagePickerController爲了從相冊中獲取圖像,我正面臨一個奇怪的行爲。呈現UIImagePickerController後應用程序崩潰

如果我在viewDidLoad中啓動圖像選擇器,它的工作原理:

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

} 

但是,如果我把initPicker()@IBAction,並從一個按鈕來調用它,點擊它後,圖像選擇器出現,但突然該應用程序與此錯誤崩潰:

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

我正在模擬器與XCode測試版6

任何想法?

+0

你可以發佈IBAction的代碼嗎? – Akaino 2014-08-29 10:41:36

+0

@Aainain,它的代碼完全一樣,但是將** initPicker()**轉換爲** viewDidLoad()**會消失。另外,** initPicker()**函數定義成爲** @ IBAction func initPicker(){...} ** – dlao 2014-08-29 14:39:06

回答

0

在測試了一堆不成功的方法之後,我改變了函數簽名,並且它可以工作!

看起來init前綴混淆了Swift。

我正在尋找一些在論壇和官方文檔相關的話題,但我沒有得到任何東西。

也許是一個錯誤?

0

嘗試聲明的對象UIImagePickerController作爲實例級別參數而不是函數內。

+0

我已經測試過沒有成功:( – dlao 2014-08-29 09:39:57

相關問題