我的应用程序总是崩溃,我不知道为什么。我正在一部分接一部分地处理选项卡式应用程序,并在每次完成一部分时对其进行测试。现在,我正在尝试从用户的设备导入图像,但似乎无法获得它。
我目前使用的是Xcode10.2.1,我知道委托方法有一些变化,我已经对它们进行了更改。它成功地构建,但每当我点击我想要导入图像的特定选项卡时,它就崩溃了。
class UserImage: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var imagePickerController : UIImagePickerController!
@IBOutlet var ImageView: UIImageView!
@IBAction func Edit(_ sender: Any) {
imagePickerController.delegate = self
imagePickerController.sourceType = .photoLibrary
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
{
if let image = info[.originalImage] as? UIImage {
ImageView.image = image
} else {
print("Take another")
}
self.dismiss(animated: true, completion: nil)
}发布于 2019-05-23 04:41:10
由于您已将imagePickerController标记为非可选值,因此当您尝试引用它但它仍然是nil时,您的应用程序将崩溃。
imagePickerController = UIImagePickerController()编辑:或者,正如@rmaddy所提到的,您可以只将控制器设置为函数的局部变量。在您的示例中,不需要将其设置为类的属性。本质上,您只需从类的顶部移除声明,而在函数内部声明它:
let imagePickerController = UIImagePickerController()https://stackoverflow.com/questions/56264633
复制相似问题