简体   繁体   English

如何使用 Swift 4 从 CMSampleBuffer 创建数据

[英]How to create Data from CMSampleBuffer With Swift 4

We can convert CMSampleBuffer to NSData with following function.我们可以使用以下函数将 CMSampleBuffer 转换为 NSData。 But we could not find a way to convert it to Data.但是我们找不到将其转换为数据的方法。

We tried using我们尝试使用

   Data(bytes: <#T##UnsafeRawPointer#>, count: <#T##Int#>)

instead of代替

   NSData(bytes: <#T##UnsafeRawPointer?#>, length: <#T##Int#>)

but no luck.但没有运气。 Is there anyone who could do it.有没有人可以做到。

func frameData() -> NSData {

        let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)

        CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))

        let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
        let height = CVPixelBufferGetHeight(imageBuffer!)
        let src_buff = CVPixelBufferGetBaseAddress(imageBuffer!)
        let nsdata = NSData(bytes: src_buff, length: bytesPerRow * height)

        CVPixelBufferUnlockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))


        return nsdata

    }

Just used刚用过

       let data = Data(bytes: src_buff!, count: bytesPerRow * height)

instead of代替

     let nsdata = NSData(bytes: src_buff, length: bytesPerRow * height)  

The key is here was !关键在这里了! after src_buff for Data.在数据的 src_buff 之后。 Because xCode was showing some errors which is not related !因为 xCode 显示了一些不相关的错误! usage I could not understand !用法我看不懂! was needed.需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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