簡體   English   中英

如何快速將CMSampleBuffer轉換為CMAttachmentBearer

[英]How to convert CMSampleBuffer to CMAttachmentBearer in swift

我是新手,我想在(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection委托中調用函數CMCopyDictionaryOfAttachments

我的代碼:

// MARK: Delegates

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
    // got an image
    let pixelBuffer : CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)
    let attachments : CFDictionaryRef = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, CMAttachmentMode( kCMAttachmentMode_ShouldPropagate)) as CFDictionaryRef!

}

這是xcode的錯誤: 'CMSampleBuffer' is not identical to 'CMAttachmentBearer'所以我如何使用sampleBuffer作為目標,如果用Objective-C編寫,此代碼就可以工作

我猜代碼中的主要問題是您傳遞了CMSampleBuffer而不是CVPixelBufferRef

接下來的問題是CMCopyDictionaryOfAttachments返回一個非托管實例,需要使用takeRetainedValue()對其進行轉換。

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
    // got an image
    let pixelBuffer : CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)
    let attachments : [NSObject : AnyObject] = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, pixelBuffer, CMAttachmentMode( kCMAttachmentMode_ShouldPropagate)).takeRetainedValue()

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM