我試圖提出一個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
任何想法?
你可以發佈IBAction的代碼嗎? – Akaino 2014-08-29 10:41:36
@Aainain,它的代碼完全一樣,但是將** initPicker()**轉換爲** viewDidLoad()**會消失。另外,** initPicker()**函數定義成爲** @ IBAction func initPicker(){...} ** – dlao 2014-08-29 14:39:06