gpt4 book ai didi

swift 3 应用程序试图以模态方式呈现主动 Controller 相机

转载 作者:搜寻专家 更新时间:2023-11-01 06:17:04 25 4
gpt4 key购买 nike

我可以点击一个按钮并拉起相机胶卷并选择一张图片并让它填充一个空的 imageView。

接下来,我尝试添加一个按钮,让用户可以使用相机。

我正在学习教程,并且为这两个选项设置了安全规则。但是我仍然遇到崩溃

Application tried to present modally an active controller

我搜索了 SO 并找到了建议我更换线路的答案

present(picker,animated: true,completion: nil)

self.navigationController?.pushViewController(picker, animated: true)

但这只会导致另一个错误。

这是我拉动相机的完整代码

picker.allowsEditing = true
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)

有什么想法吗?

编辑

我的代码现在看起来像这样

func useCamera() {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = false
picker.sourceType = .camera
present(picker, animated: true, completion: nil)
}

这个我也试过

    func useCamera() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = false
picker.sourceType = .camera
present(picker, animated: true, completion: nil)
}
}

但现在发生的是它只是打开相机胶卷,我在控制台中看到了这个

Attempt to present UIImagePickerController: 0x1018c4000 on Lended.AddAccountViewController: 0x10116f540 whose view is not in the window hierarchy!

但应用程序不会崩溃,这很好

最佳答案

This is my full code for pulling the camera

嗯嗯,不,不是。您一直在引用 picker。这必然表明某些变量或属性 picker 已经存在。所以有其他代码初始化该变量,并且可能有其他代码使用该变量。

我认为,这就是问题的根源。不知何故,你不小心使用了这个 View Controller 。问题源于长期持有 UIImagePickerController。不。这样做的代码总是会遇到问题。只需在使用之前创建一个选择器的实例:

let picker = UIImagePickerController() // *
picker.allowsEditing = true
picker.sourceType = .camera
// picker.cameraCaptureMode = .photo
// picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)

现在我将向您展示一些实际工作的真实代码:

let src = UIImagePickerControllerSourceType.camera
guard UIImagePickerController.isSourceTypeAvailable(src) else {print("no camera"); return}
guard let arr = UIImagePickerController.availableMediaTypes(for:src) else {print("no types"); return}

let picker = UIImagePickerController()
picker.mediaTypes = arr
picker.allowsEditing = true
picker.delegate = self
self.present(picker, animated: true)

关于swift 3 应用程序试图以模态方式呈现主动 Controller 相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42354765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com