ios - 如何在 swift 中将 CMSampleBuffer 转换为 CMAttachmentBearer

标签 ios swift avfoundation

我是 swift 的新手,我想在 (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 中调用函数 CMCopyDictionaryOfAttachments 委托(delegate)

我的代码:

// 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' 与 '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()

}

关于ios - 如何在 swift 中将 CMSampleBuffer 转换为 CMAttachmentBearer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27755958/

相关文章:

ios - Collection View :layout:sizeForItemAt not called swift 4 iOS 11

ios - 如何在堆栈 View 中动态调整按钮的大小

ios - 在 iOS 中绘制多点路线

json - 使用 SwiftyJSON 的 Swift 可失败初始化器

ios - IBDesignable View 内的 IBInspectable 按钮

ios - 从 AVCaptureVideoDataOutput 获取正确的第一帧 - AVFoundation

ios - 尝试实现 Facebook 即时文章页面,但我无法获取 ImageView 高度以进行动画处理

ios - SDWebImage 按钮背景图像调整大小

iphone - 使用 AVMutableVideoCompositionLayerInstruction 旋转视频

swift - iOS 13 Beta - 相机捕捉以错误的方向保存照片