gpt4 book ai didi

ios - UIImagePickerController fatal error : unexpectedly found nil while unwrapping an Optional value

转载 作者:行者123 更新时间:2023-11-28 08:55:56 25 4
gpt4 key购买 nike

我有以下代码:

func startCameraFromViewController(viewController: UIViewController, withDelegate delegate:
protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>) -> Bool {

if (UIImagePickerController.isSourceTypeAvailable(.Camera) == false) {
return false
}

let cameraController = UIImagePickerController()
cameraController.sourceType = .Camera
cameraController.mediaTypes = [kUTTypeMovie as String]
cameraController.allowsEditing = false
cameraController.delegate = delegate

presentViewController(cameraController, animated: true, completion: nil)
return true
}

它被调用如下:

func recordVideoButtonTapped() {
startCameraFromViewController(self, withDelegate: self)
}

但由于某些原因,我收到以下错误

fatal error: unexpectedly found nil while unwrapping an Optional value

在这一行:

 presentViewController(cameraController, animated: true, completion: nil)

请帮忙调试

这个问题不同于

What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

这是一个与展开可选值有关的更普遍的问题

这个问题专门针对 UIImagePickerController

最佳答案

我使用的是 Xcode 6.4,它工作正常:

import UIKit
import MobileCoreServices

class FirstViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
}

func startCameraFromViewController(viewController: UIViewController, withDelegate delegate:
protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>) -> Void {

if (UIImagePickerController.isSourceTypeAvailable(.Camera) == false) {
println("fail")
//return false
}

let cameraController = UIImagePickerController()
cameraController.sourceType = .Camera
cameraController.mediaTypes = [kUTTypeMovie as String]
cameraController.allowsEditing = false
cameraController.delegate = delegate

presentViewController(cameraController, animated: true, completion: nil)
println("ok")
//return true
}

@IBAction func ToSecondPressed(sender: AnyObject) {
startCameraFromViewController(self, withDelegate: self)
}
}

关于ios - UIImagePickerController fatal error : unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33058700/

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