我的应用程序中有一个自定义摄像头,它工作正常,但在新的更新后,我遇到了以下错误:

"jpegPhotoDataRepresentation(forJPEGSampleBuffer:PreviewPhotosSampleBuffer:)"在iOS 11.0中被弃用:改为使用-[AVCapturePhoto fileDataRepresentation].

这是我得到错误的地方:

    guard let imageData =
        AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
            return
    }

这是我的全部功能(如果需要):

//Take pic function
func photoOutput(_ captureOutput: AVCapturePhotoOutput,
                 didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?,
                 previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?,
                 resolvedSettings: AVCaptureResolvedPhotoSettings,
                 bracketSettings: AVCaptureBracketedStillImageSettings?,
                 error: Error?) {


    // Make sure we get some photo sample buffer
    guard error == nil,
        let photoSampleBuffer = photoSampleBuffer else {
            print("Error capturing photo: \(String(describing: error))")
            return
    }
    // Convert photo same buffer to a jpeg image data by using // AVCapturePhotoOutput
    guard let imageData =
        AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
            return
    }

    let dataProvider = CGDataProvider(data: imageData as CFData)

    let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.absoluteColorimetric)


    let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)

    self.tempImageView.image = image

}

我的目标是:What should I use instead to make it work?

非常感谢.

推荐答案

在iOS 11中,您应该这样使用:

@available(iOS 11.0, *)
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        let imageData = photo.fileDataRepresentation()
}

Swift相关问答推荐

在swift静态var扩展中,如何或可以访问和返回具体类?

显示第二个操作紧接在另一个操作后的工作表在SwiftUI中不起作用

为什么ClosedRange<;Int&>包含的速度比预期慢340万倍?

如何使泡泡上的箭头直达封闭矩形

';NSInternal不一致异常';,原因:';可见导航栏Xcode 15.0 Crash请求布局

相互取消任务

如何使用可搜索在搜索栏中搜索包括>或<?

允许视图在视图内更改

在表单中对齐文本框

类型擦除`AsyncMapSequence, Element>`

仅当 boolean 为真时才实现方法(application:didReceiveRemoteNotification)

在 Swift 中增量写入大型文本文件的最佳方法

在 Swift 5.5 中编写同步和异步函数

`@available` 属性在 macOS 版本上被错误地限制

如何在 Swift 中将 XPC 自定义类列入白名单?

如何使被拖动的对象成为前景(foreground)对象

Swift - 获取本地日期和时间

Xcode 7.3 Swift 的语法高亮和代码完成问题

Swift 自定义字体 Xcode

保持窗口始终在顶部?