无法使用 AVCaptureDevice 发现相机

Unable to discover camera using 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).

你可以试试

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