当前位置: 首页 > 知识库问答 >
问题:

UIImagePickerController致命错误:在展开可选值时意外发现nil

颜瀚漠
2023-03-14

我有以下代码:

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

但是由于某种原因,我得到了以下错误

致命错误:在展开可选值时意外发现nil

在这一行:

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

请帮忙调试。

这个问题不同于

“致命错误:在展开可选值时意外发现为零”是什么意思?

这是一个更普遍的问题,与期权的展开有关

此问题特别适用于UIImagePickerController

共有1个答案

祁博雅
2023-03-14

我使用的是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)  
    }
}
 类似资料: