无法使用AVCaptureDevice发现相机

时间:2019-01-18 17:11:24

标签: swift avcapturedevice

这是我的代码:

if AVCaptureDevice.authorizationStatus(for: AVMediaType.video) == .authorized {
    let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(
        deviceTypes: [.builtInDualCamera],
        mediaType: AVMediaType.video,
        position: .back)
    let captureDevice = deviceDiscoverySession.devices.first
}

结果为空数组(deviceDiscoverySession.devices)。 我正在实际设备(iPhone 6+ iOS 12.1.2)上运行。

1 个答案:

答案 0 :(得分:1)

您可以尝试

// Find a camera with the specified AVCaptureDevicePosition, returning nil if one is not found
func cameraWithPosition(position: AVCaptureDevice.Position) -> AVCaptureDevice? {
    let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .unspecified)
    for device in discoverySession.devices {
        if device.position == position {
            return device
        }
    }

    return nil
}

if let newCamera = cameraWithPosition(position: .back) {
   //
}