ios - iPhone 7+,ios 11.2 : Depth data delivery is not supported in the current configuration

标签 ios swift avcapturesession avcapturedevice avcaptureoutput

这个错误让我抓狂。我正在尝试生成绝对最小的代码,以使用其 DualCam 从 iPhone 7+ 获取 AVDepthData

我有这个代码:


//
//  RecorderViewController.swift
//  ios-recorder-app


import UIKit
import AVFoundation


class RecorderViewController: UIViewController {

    @IBOutlet weak var previewView: UIView!

    @IBAction func onTapTakePhoto(_ sender: Any) {

        guard let capturePhotoOutput = self.capturePhotoOutput else { return }

        let photoSettings = AVCapturePhotoSettings()

        photoSettings.isDepthDataDeliveryEnabled = true //Error

        capturePhotoOutput.capturePhoto(with: photoSettings, delegate: self)

    }

    var session: AVCaptureSession?
    var videoPreviewLayer: AVCaptureVideoPreviewLayer?
    var capturePhotoOutput: AVCapturePhotoOutput?


    override func viewDidLoad() {
        super.viewDidLoad()

        AVCaptureDevice.requestAccess(for: .video, completionHandler: { _ in })

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

        do {
            print(captureDevice!)
            let input = try AVCaptureDeviceInput(device: captureDevice!)

            self.capturePhotoOutput = AVCapturePhotoOutput()
            self.capturePhotoOutput?.isDepthDataDeliveryEnabled = true //Error

            self.session = AVCaptureSession()
            self.session?.addInput(input)

            self.videoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.session!)
            self.videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
            self.videoPreviewLayer?.frame = view.layer.bounds
            previewView.layer.addSublayer(self.videoPreviewLayer!)

            self.session?.addOutput(self.capturePhotoOutput!)
            self.session?.startRunning()

        } catch {
            print(error)
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension RecorderViewController : AVCapturePhotoCaptureDelegate {

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        print(photo.depthData)

    }


}

如果我注释掉标有“错误”的行,代码将按我预期的方式工作,并为 depthData 打印 nil

但是,保持原样,我得到一个异常。错误消息指出:AVCapturePhotoOutput setDepthDataDeliveryEnabled:] 当前配置不支持深度数据传输

如何更改“当前配置”以支持深度交付?

我看过这个视频:https://developer.apple.com/videos/play/wwdc2017/507/这很有帮助,我相信我已经按照完成这项工作所需的确切步骤进行操作。

如有任何提示,我们将不胜感激!

最佳答案

有两件事我需要解决。

  1. sessionPreset 设置为支持深度的格式,例如 .photo
  2. 在设置 .isDepthDataDeliveryEnabled = true 之前将 cameraPhotoOutput 添加到 session 。

这是我获取照片深度的最少代码:


//
//  RecorderViewController.swift
//  ios-recorder-app
//

import UIKit
import AVFoundation


class RecorderViewController: UIViewController {

    @IBOutlet weak var previewView: UIView!

    @IBAction func onTapTakePhoto(_ sender: Any) {

        guard var capturePhotoOutput = self.capturePhotoOutput else { return }

        var photoSettings = AVCapturePhotoSettings()
        photoSettings.isDepthDataDeliveryEnabled = true

        capturePhotoOutput.capturePhoto(with: photoSettings, delegate: self)

    }

    var session: AVCaptureSession?
    var videoPreviewLayer: AVCaptureVideoPreviewLayer?
    var capturePhotoOutput: AVCapturePhotoOutput?


    override func viewDidLoad() {
        super.viewDidLoad()

        AVCaptureDevice.requestAccess(for: .video, completionHandler: { _ in })

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

        print(captureDevice!.activeDepthDataFormat)

        do{
            let input = try AVCaptureDeviceInput(device: captureDevice!)

            self.capturePhotoOutput = AVCapturePhotoOutput()

            self.session = AVCaptureSession()
            self.session?.beginConfiguration()
            self.session?.sessionPreset = .photo
            self.session?.addInput(input)

            self.videoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.session!)
            self.videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
            self.videoPreviewLayer?.frame = self.view.layer.bounds
            self.previewView.layer.addSublayer(self.videoPreviewLayer!)

            self.session?.addOutput(self.capturePhotoOutput!)
            self.session?.commitConfiguration()
            self.capturePhotoOutput?.isDepthDataDeliveryEnabled = true
            self.session?.startRunning()
        }
        catch{
            print(error)
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension RecorderViewController : AVCapturePhotoCaptureDelegate {

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        print(photo.depthData)
    }


}

关于ios - iPhone 7+,ios 11.2 : Depth data delivery is not supported in the current configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49302065/

相关文章:

ios - NSPredicate 按单词匹配字符串

ios - 在 iOS 中复制和添加 View 和内容

ios - 如何将本地 html 文件加载到 UIWebView

ios - 选取图像后 imageview 不更新

swift - iOS 在 MapBox map 上移动注释

ios - iPhone相机无法设置长时间曝光

ios - UIInterfaceOrientation 是 Landscape 但 UIView 仍处于 Portrait 模式

ios - 拍摄 AVCaptureVideoPreviewLayer View 的快照

iphone - 电影播放器​​完成通知未在iOS SDK中启动

ios - 从 Apple 登录注销