【问题标题】:Why is photo library not displaying when I select it in swift?为什么我在swift中选择照片库时不显示照片库?
【发布时间】:2021-03-20 00:51:33
【问题描述】:

使用this 问题我要求用户决定是要使用相机还是从手机中选择图像:

//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
        
    }

但是,当我对此进行测试时,会弹出警报并选择照片库,但该库没有出现。我曾尝试使用viewDidAppear,但也没能奏效。没有错误出现,它只是隐藏警报并显示当前视图控制器。

【问题讨论】:

    标签: ios swift photo-gallery


    【解决方案1】:

    您可能会想象当警报出现时代码会停止。但事实并非如此!而且您不能同时显示两个呈现的视图控制器。但这就是你想要做的。

    您说的是showAlert(),所以现在您的警报已启动,然后您立即说present(imagePicker),您不能这样做,因为您的警报仍在启动。

    使用showAlert 警报的操作处理程序来显示图像选择器。这样,警报关闭,图像选择器打开。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多