如何实例化AVCaptureDevice

时间:2015-02-06 21:11:40

标签: ios xcode swift avcapturesession avcapturedevice

我收到错误说:

  

无法直接实例化AVCaptureDevice

当我试图将相机设置为前置摄像头时。我的代码如下所示:

        let inputDevice:AVCaptureDevice = setCameraPosition()

        var error:NSError?
        let deviceInput:AVCaptureDeviceInput = AVCaptureDeviceInput.deviceInputWithDevice(inputDevice, error: &error) as AVCaptureDeviceInput

func setCameraPosition()->AVCaptureDevice{
    let devices = AVCaptureDevice.devices()
    var captureDevice = AVCaptureDevice()

    // Loop through all the capture devices on this phone
    for device in devices {
        // Make sure this particular device supports video
        if (device.hasMediaType(AVMediaTypeVideo)) {
            // Finally check the position and confirm we've got the back camera
            if(device.position == AVCaptureDevicePosition.Front) {
                captureDevice = device as AVCaptureDevice
            }
        }
    }
    return captureDevice
}

我可以使用这行代码来使用后置摄像头:

let inputDevice:AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

我无法弄清楚它出错的地方。

有关如何继续的任何建议将不胜感激。

0 个答案:

没有答案