简体   繁体   中英

How to convert CMSampleBuffer to CMAttachmentBearer in swift

I'm new of swift, I want to call function CMCopyDictionaryOfAttachments in (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection delegate

Codes of mine:

// 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!

}

This got an error by xcode : 'CMSampleBuffer' is not identical to 'CMAttachmentBearer' so how can I use sampleBuffer as target, this code works if written in objective-c

I guess the major problem in your code is that you passe the CMSampleBuffer instead of the CVPixelBufferRef .

The next problem then is that CMCopyDictionaryOfAttachments returns an unmanaged instance, which needs to be converted using 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()

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM