var captureSession = AVCaptureSession()
override func viewDidLoad() {
super.viewDidLoad()
AVCaptureDevice.requestAccess(for: .video) { (succ) in
print("\(succ) video")
}
}
@IBAction func Shoot(_ sender: Any) {
if captureSession.isRunning == true {
captureSession.stopRunning()
}
let cam = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position: .back)
captureSession = AVCaptureSession()
captureSession.beginConfiguration()
do {
let input = try AVCaptureDeviceInput(device: cam.devices.first!)
captureSession.addInput(input)
} catch {
print(error)
}
captureSession.sessionPreset = .photo
let output = AVCapturePhotoOutput()
captureSession.addOutput(output)
captureSession.commitConfiguration()
output.isDepthDataDeliveryEnabled = true
output.isHighResolutionCaptureEnabled = true
captureSession.startRunning()
}
@IBAction func Go(_ sender: Any) {
let output = captureSession.outputs.first as! AVCapturePhotoOutput
output.capturePhoto(with: photoSettingsCreator(), delegate: self)
}
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if error != nil {
print(error!)
}
let dat = photo.fileDataRepresentation()!
let img = UIImage(data: dat)
print(photo.depthData!)
}
func photoSettingsCreator() -> AVCapturePhotoSettings {
let settings = AVCapturePhotoSettings(format: [AVVideoCodecKey : AVVideoCodecType.hevc, AVVideoCompressionPropertiesKey : [AVVideoQualityKey : 1.0]])
settings.isDepthDataDeliveryEnabled = true
settings.isHighResolutionPhotoEnabled = true
settings.isAutoStillImageStabilizationEnabled = true
settings.flashMode = .off
return settings
}
直到我在手机上安装了iOS 12 Beta为止,它都可以正常捕获深度数据,但是现在出现错误:
Error Domain = AVFoundationErrorDomain代码= -11800“操作无法完成” UserInfo = {NSLocalizedFailureReason =发生未知错误(-16800),NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x283094510 {Error Domain = NSOSStatusErrorDomain Code = -16800“(null)”}}
我不知道此错误来自何处。错误显示在照片输出的if error
部分。
答案 0 :(得分:0)
这可能是因为您的captureSession
的初始化。关闭视图时,最好在viewWillDisappear
self.captureSession.deinitialize()