如何使用手机默认相机

how to use the phones defualt camera

我正在尝试在我的应用程序中使用我的 iPhone 相机。我使用 xcode 11 和 swift 5

错误发生在以下行中:

let usecamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

错误信息:

Type 'AVCaptureDevice' has no member 'defaultDevice'

我尝试了下面的代码,但它不起作用:

 let captureDevice = AVCaptureDevice.default(.builtInDualCamera,
    for: AVMediaType.video,
    position: .back)

    do {
        let input = try AVCaptureDeviceInput(device: captureDevice)
        session.addInput(input)

    }

但收到错误消息:

Value of optional type 'AVCaptureDevice?' must be unwrapped to a value of type 'AVCaptureDevice'

错误 1 ​​修复

您需要将行更新到最新 swift

let usecamera = AVCaptureDevice.default(for: .video)

错误 2 修复

如错误所述 captureDevice 是可选的,您需要使用 captureDevice! 或安全地 guard ,因此替换

let captureDevice = AVCaptureDevice.default(.builtInDualCamera,
for: AVMediaType.video,
position: .back)

guard  let captureDevice = AVCaptureDevice.default(.builtInDualCamera,
for: AVMediaType.video,
position: .back) else { return }