swift 首次打开UIImagePickerController时速度较慢,双击时除外

4smxwvx5  于 2023-01-25  发布在  Swift
关注(0)|答案(3)|浏览(470)

我在iOS 9上遇到了这种非常奇怪的行为,我有一个tableViewCell,当点击它来拍照时,它会打开一个imagePicker,当你第一次点击单元格时,它需要大约10秒才能打开选取器,但当你点击它两次时,它会立即打开...
拾取器的初始化代码如下所示

let certificateImagePicker = UIImagePickerController()
certificateImagePicker.delegate = self
certificateImagePicker.allowsEditing = false
certificateImagePicker.sourceType = .Camera
certificateImagePicker.modalPresentationStyle = .CurrentContext

显示选取器的代码为presentViewController(certificateImagePicker, animated: false, completion: nil)
我现在不知道它是否相关,但打开拾取器后,它显示此错误消息
对尚未呈现的视图进行快照将导致快照为空。请确保在快照之前或在屏幕更新之后至少呈现一次视图。

fjaof16o

fjaof16o1#

我在第一次尝试显示UIImagePickerController时也遇到了类似的延迟。在我的情况下,帮助很大的是在初始化父UIViewController的同时初始化它,如下所示:

class ExampleViewController: UIViewController, UIImagePickerControllerDelegate {
  let imagePicker = UIImagePickerController()

  func presentImagePicker() {
    imagePicker.delegate = self
    imagePicker.allowsEditing = false
    imagePicker.sourceType = .camera
    imagePicker.modalPresentationStyle = .currentContext
    self.present(imagePicker, animated: false, completion: nil)
  }
}
ghhaqwfi

ghhaqwfi2#

这仅适用于调试版本,当您运行连接到Xcode的应用时。

mec1mxoz

mec1mxoz3#

private var imagePickerController = UIImagePickerController()
...
@objc func addPhoto() {
 imagePickerController.delegate = self
 imagePickerController.sourceType = .savedPhotosAlbum
        
  present(imagePickerController, animated: true)
}
...

当我有这个实现时,它在等待,当第一个礼物被尝试,然后在改变我的变量为lazy variable后,然后它像魅力一样工作。

private lazy var imagePickerController = UIImagePickerController()

相关问题