Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios AVFoundation:切换相机在CanadInput时失败_Ios_Swift_Camera_Toggle_Avfoundation - Fatal编程技术网

Ios AVFoundation:切换相机在CanadInput时失败

Ios AVFoundation:切换相机在CanadInput时失败,ios,swift,camera,toggle,avfoundation,Ios,Swift,Camera,Toggle,Avfoundation,我正在尝试添加带有AVFoundation的旋转摄影机功能,以允许用户在前向和后向摄影机之间切换 如下面的代码所示,我加入了一些println()语句,所有的值似乎都是合法的,但在测试CanAddInput()时,代码总是会下降到失败的else子句 我已经尝试将sessionPreset(在另一个预先初始化会话的函数中)设置为各种值,包括AVCaptureSessionPresetHigh和AVCaptureSessionPresetLow,但没有帮助 @IBAction func rotate

我正在尝试添加带有
AVFoundation
的旋转摄影机功能,以允许用户在前向和后向摄影机之间切换

如下面的代码所示,我加入了一些
println()
语句,所有的值似乎都是合法的,但在测试
CanAddInput()
时,代码总是会下降到失败的else子句

我已经尝试将sessionPreset(在另一个预先初始化会话的函数中)设置为各种值,包括
AVCaptureSessionPresetHigh
AVCaptureSessionPresetLow
,但没有帮助

@IBAction func rotateCameraPressed(sender: AnyObject) {

    // Loop through all the capture devices to find right ones
    var backCameraDevice : AVCaptureDevice?
    var frontCameraDevice : AVCaptureDevice?
    let devices = AVCaptureDevice.devices()
    for device in devices {
        // Make sure this particular device supports video
        if (device.hasMediaType(AVMediaTypeVideo)) {
            // Define devices
            if (device.position == AVCaptureDevicePosition.Back) {
                backCameraDevice = device as? AVCaptureDevice
            } else if (device.position == AVCaptureDevicePosition.Front) {
                frontCameraDevice = device as? AVCaptureDevice
            }
        }
    }

    // Assign found devices to corresponding input
    var backInput : AVCaptureDeviceInput?
    var frontInput : AVCaptureDeviceInput?
    var error: NSError?
    if let backDevice = backCameraDevice {
        println("Back device is \(backDevice)")
        backInput = AVCaptureDeviceInput(device : backDevice, error: &error)
    }
    if let frontDevice = frontCameraDevice {
        println("Front device is \(frontDevice)")
        frontInput = AVCaptureDeviceInput(device : frontDevice, error: &error)
    }

    // Now rotate the camera
    isBackCamera = !isBackCamera  // toggle camera position
    if isBackCamera {
        // remove front and add back
        captureSession!.removeInput(frontInput)
        if let bi = backInput {
            println("Back input is \(bi)")
            if captureSession!.canAddInput(bi) {
                captureSession!.addInput(bi)
            } else {
                println("Cannot add back input!")
            }
        }
    } else {
        // remove back and add front
        captureSession!.removeInput(backInput)
        if let fi = frontInput {
            println("Front input is \(fi)")
            if captureSession!.canAddInput(fi) {
                captureSession!.addInput(fi)
            } else {
                println("Cannot add front input!")
            }
        }
    }
}

问题似乎在于,从迭代中找到的设备派生的输入实际上与captureSession变量中的输入不匹配。这似乎是一件新鲜事,因为我看到的所有关于它的代码都会通过迭代设备列表来查找并删除当前相机的输入,就像我在代码中所做的那样

这似乎不再有效了——好吧,至少在我发布的代码中不起作用,它基于我能够挖掘的所有源代码(这些源代码恰好都在Objective C中)。canAddInput()失败的原因是removeInput()从未成功;奇怪的是,它没有发出关于不能拥有多个输入设备的常见错误(因为这有助于调试)

无论如何,修复方法是不从找到的设备(用于工作)中删除派生输入上的输入。相反,通过进入captureSession.inputs变量并对其执行removeInput()来移除实际存在的输入设备

为了消除所有的代码混乱,我做了以下几点:

for ii in captureSession!.inputs {
  captureSession!.removeInput(ii as! AVCaptureInput)
}
这就成功了!:)