四年前,我使用了現在已棄用的這種型別方法(以及它的AVCaptureStillImageOutput類)。
func jpegStillImageNSDataRepresentation(_ jpegSampleBuffer: CMSampleBuffer) -> Data? { }
let data = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(jpegBuffer)
AVCapturePhotoOutput旨在替換該類的類AVCaptureStillImageOutput在其庫中沒有所需的方法。我應該使用什么方法而不是棄用的方法?
uj5u.com熱心網友回復:
所以你確實需要實作AVCapturePhotoOutput,比如(非常粗略的實作想法,請遵循Apple 的指南):
class PhotoCapturer {
var photoOutput = AVCapturePhotoOutput()
func takePhoto() {
let photoSettings: AVCapturePhotoSettings = {
let processedFormat: [String: Any] = [AVVideoCodecKey: AVVideoCodecType.jpeg]
return AVCapturePhotoSettings(format: processedFormat)
}()
photoOutput.capturePhoto(with: photoSettings, delegate: self)
}
}
如您所見,我們還需要實作AVCapturePhotoCaptureDelegate委托,如下所示:
extension PhotoCapturer: AVCapturePhotoCaptureDelegate {
/// Receives this message whenever a captured image is ready
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
拍攝影像時,此方法將以AVCapturePhoto格式接收它。從這里您有多種選擇,具體取決于您的需要:
- .fileDataRepresentation() - 適合保存在檔案中的平面資料表示。由于我們要求使用上述
jpeg格式,因此資料也將代表 jpeg。 - .cgImageRepresentation() -
CGImage表示。如果您想顯示或編輯影像,非常適合。不完全是JPEG,但可以使用JPEG轉換UIImage(cgImage: ...).jpegData(compressionQuality:...) - 最后,還有pixelBuffer
PixelBuffer,如果您希望重用現有處理的某些部分,它會將您回傳到熟悉的格式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/504401.html
