Ios 为什么在swift中选择照片库时不显示它?
使用问题我要求用户决定是使用相机还是从手机中选择图像:Ios 为什么在swift中选择照片库时不显示它?,ios,swift,photo-gallery,Ios,Swift,Photo Gallery,使用问题我要求用户决定是使用相机还是从手机中选择图像: //Show alert to select the media source type. private func showAlert() { let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferre
//Show alert to select the media source type.
private func showAlert() {
let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action: UIAlertAction) in
self.imagePicker.sourceType = .camera
}))
alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: {(action: UIAlertAction) in
self.imagePicker.sourceType = .photoLibrary
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
self.present(alert, animated: true, completion: nil)
}
我在viewDidLoad
中查看了它,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
firstTextField.delegate = self
showAlert()
present(imagePicker, animated: true, completion: nil)
imagePicker.delegate = self
firstImageView.layer.cornerRadius = 8
}
但是,当我测试此功能时,会弹出警报,并选择照片库,但该库不会出现。我尝试过使用
viewdide
,但同样失败。没有出现错误,它只是隐藏警报并显示当前视图控制器。您可能会想象当警报出现时代码停止。但事实并非如此!并且不能同时显示两个显示的视图控制器。但这正是你想要做的
您说的是showarter()
,因此现在您的警报已启动,然后您立即说的是present(imagePicker)
,这是您无法执行的,因为您的警报仍处于启动状态
使用showAlert
警报的操作处理程序显示图像选择器。这样,警报关闭,图像选择器打开