swift 3应用程序试图以模态方式呈现活动控制器摄像机

swift 3应用程序试图以模态方式呈现活动控制器摄像机,swift,Swift,我可以点击一个按钮,拉起相机卷,选择一个图像,让它填充一个空的图像视图 接下来,我尝试添加一个按钮,让用户使用相机 我正在学习一个教程,我已经为这两个选项设置了安全规则。然而,我仍然得到一个崩溃说 应用程序试图以模态方式呈现活动控制器 我搜索了这么多,找到了建议我更换线路的答案 present(picker,animated: true,completion: nil) 与 然而,这只会导致另一个错误 这是我拉相机的全部代码 picker.allowsEditing = true picker

我可以点击一个按钮,拉起相机卷,选择一个图像,让它填充一个空的图像视图

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

我正在学习一个教程,我已经为这两个选项设置了安全规则。然而,我仍然得到一个崩溃说

应用程序试图以模态方式呈现活动控制器

我搜索了这么多,找到了建议我更换线路的答案

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

然而,这只会导致另一个错误

这是我拉相机的全部代码

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)
    }
  }
但现在发生的事情是,它只是打开了摄像机的镜头,在控制台上我看到了这个

尝试在上显示UIImagePickerController:0x1018c4000 Lended.AddAccountViewController:0x10116f540,其视图不在 窗口层次结构

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

这是我拉相机的全部代码

picker.allowsEditing = true
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)
嗯,不,不是。你一直在说皮克。这必然表明某些变量或属性选择器已经存在。因此,还有其他代码初始化该变量,也可能有其他代码使用该变量

我认为,这就是问题的根源。不知何故,您不小心使用了此视图控制器。问题源于长期使用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)
这是我拉相机的全部代码

picker.allowsEditing = true
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)
嗯,不,不是。你一直在说皮克。这必然表明某些变量或属性选择器已经存在。因此,还有其他代码初始化该变量,也可能有其他代码使用该变量

我认为,这就是问题的根源。不知何故,您不小心使用了此视图控制器。问题源于长期使用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)

多亏了@matt,我才得以摆脱我所犯的错误。 但最后我意外地为第二个按钮创建了一个双IBAction

我创建了一个简单的测试,为按钮1打印左侧,为按钮2打印右侧

当我点击左键时,它会向左打印,但当我点击右键时,它会向左和向右打印

在摄像机的上下文中…摄像机可能在一段时间前就出现了,但由于它正在运行先将摄像机卷起的功能…摄像机从来没有机会

有效的代码是这样的

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

多亏了@matt,我才得以摆脱我所犯的错误。 但最后我意外地为第二个按钮创建了一个双IBAction

我创建了一个简单的测试,为按钮1打印左侧,为按钮2打印右侧

当我点击左键时,它会向左打印,但当我点击右键时,它会向左和向右打印

在摄像机的上下文中…摄像机可能在一段时间前就出现了,但由于它正在运行先将摄像机卷起的功能…摄像机从来没有机会

有效的代码是这样的

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

在你提到的评论中:


我只是把我的VC嵌入到导航控制器中……没什么疯狂的——RubberDucky4444

我和你犯了同样的错误,那正是我的问题

给定我的导航/视图控制器:

let ccNavController = self.storyboard?.instantiateViewController(withIdentifier: "creditCardNavigationController") as! UINavigationController
guard let ccVC = ccNavController.visibleViewController as? CreditCardViewController else {
    fatalError("Unexpected controller: \(String(describing: ccNavController.visibleViewController))")
}
在为视图控制器本身调用present时,我遇到了相同的错误

self.present(ccVC, animated: true, completion: nil)
当我为导航控制器演示时,它起作用了:

self.present(ccNavController, animated: true, completion: nil)

在你提到的评论中:


我只是把我的VC嵌入到导航控制器中……没什么疯狂的——RubberDucky4444

我和你犯了同样的错误,那正是我的问题

给定我的导航/视图控制器:

let ccNavController = self.storyboard?.instantiateViewController(withIdentifier: "creditCardNavigationController") as! UINavigationController
guard let ccVC = ccNavController.visibleViewController as? CreditCardViewController else {
    fatalError("Unexpected controller: \(String(describing: ccNavController.visibleViewController))")
}
在为视图控制器本身调用present时,我遇到了相同的错误

self.present(ccVC, animated: true, completion: nil)
当我为导航控制器演示时,它起作用了:

self.present(ccNavController, animated: true, completion: nil)

您会注意到我还注释了您的modalPresentationStyle设置,这可能是不必要的。还有。照片是默认的捕获模式,所以你也不需要。好的,我把初始化代码放在上面,因为我在两个地方使用过它,我认为那是“干燥剂”,我回家后会尝试这个代码,这很好!我们已经解决了坠机事件。现在你需要弄清楚为什么相机已经在使用了。顺便说一句,你需要设置一个委托。好的,你的问题很严重。您说的是self.present,但self是不在视图控制器层次结构中的视图控制器。你和你的关系很深
你的视图控制器。你需要弄清楚这是怎么发生的,这与你正在展示的代码无关。我只是把我的VC嵌入到导航控制器中……没什么疯狂的。你会注意到我还注释了你的modalPresentationStyle设置,这可能是不必要的。还有。照片是默认的捕获模式,所以你也不需要。好的,我把初始化代码放在上面,因为我在两个地方使用过它,我认为那是“干燥剂”,我回家后会尝试这个代码,这很好!我们已经解决了坠机事件。现在你需要弄清楚为什么相机已经在使用了。顺便说一句,你需要设置一个委托。好的,你的问题很严重。您说的是self.present,但self是不在视图控制器层次结构中的视图控制器。你和你的视图控制器陷入了困境。你需要弄清楚这是怎么发生的,这与你正在展示的代码无关。我只是把我的VC嵌入到一个导航控制器中……没什么疯狂的