我只想点击图片并在UIImageView上显示。
我已经定义了所有需要的插座和操作,这是代码。
但它显示错误。
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var camera: UIButton!
@IBOutlet weak var gallery: UIButton!
@IBOutlet weak var viewImage: UIImageView!
@IBAction func PhotoGalleryAction(sender: UIButton) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
viewImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismissViewControllerAnimated(true, completion: nil)
}
}
这是错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
答案 0 :(得分:1)
您必须检查故事板和代码之间的连接。 即使您已连接它,它也可能因使用旧故事板文件或其他内容而损坏(使用Assitant Editor时经常会发生这种情况)