Ios 将图片从照片库导入xcode中的应用程序
我的应用程序一直崩溃,我不知道为什么。我正在一部分一部分地开发一个选项卡式应用程序,并在每次完成一部分时对其进行测试。 现在我正在尝试从用户的设备导入图像,但我似乎无法获取它 我目前正在使用Xcode 10.2.1,我知道委托方法有一些更改,我已经更改了它们。它成功地构建了图像,但每当我点击我想导入图像的某个特定选项卡时,它就会崩溃Ios 将图片从照片库导入xcode中的应用程序,ios,swift,uiimagepickercontroller,Ios,Swift,Uiimagepickercontroller,我的应用程序一直崩溃,我不知道为什么。我正在一部分一部分地开发一个选项卡式应用程序,并在每次完成一部分时对其进行测试。 现在我正在尝试从用户的设备导入图像,但我似乎无法获取它 我目前正在使用Xcode 10.2.1,我知道委托方法有一些更改,我已经更改了它们。它成功地构建了图像,但每当我点击我想导入图像的某个特定选项卡时,它就会崩溃 class UserImage: UIViewController,UINavigationControllerDelegate, UIImagePickerCon
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)
}
它正在崩溃,因为您从未实际初始化过
UIImagePickerController
。由于您将imagePickerController
标记为非可选值,因此当您尝试引用它时,应用程序将崩溃,但它仍然是nil
。将此行添加到编辑函数的开头以初始化变量:
imagePickerController = UIImagePickerController()
编辑:或者,正如@rmaddy提到的,您可以将控制器设置为函数的局部变量。在您的示例中,不需要将其设置为类的属性。基本上,您只需从类的顶部删除声明,而是在函数中声明它:
let imagePickerController = UIImagePickerController()
它正在崩溃,因为您从未实际初始化过
UIImagePickerController
。由于您将imagePickerController
标记为非可选值,因此当您尝试引用它时,应用程序将崩溃,但它仍然是nil
。将此行添加到编辑函数的开头以初始化变量:
imagePickerController = UIImagePickerController()
编辑:或者,正如@rmaddy提到的,您可以将控制器设置为函数的局部变量。在您的示例中,不需要将其设置为类的属性。基本上,您只需从类的顶部删除声明,而是在函数中声明它:
let imagePickerController = UIImagePickerController()
如果
imagePickerController
只是Edit
函数中的一个局部变量,那就更好了。我同意,它没有必要成为这个阶级的财产。我本来打算提出这个建议,但我决定让他们来决定。如果imagePickerController
只是Edit
函数中的一个局部变量,那就更好了。我同意,它没有必要成为这个阶级的财产。我本来打算提出这个建议,但我决定让他们来做决定。每当你发布关于撞车的问题时,包括撞车的细节是非常重要的。发布完整准确的错误消息,并指出导致崩溃的确切代码行。无论何时发布有关崩溃的问题,都必须包含有关崩溃的详细信息。发布完整准确的错误消息,并指出导致崩溃的确切代码行。