We can convert CMSampleBuffer to NSData with following function. 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. Because xCode was showing some errors which is not related ! usage I could not understand ! was needed.
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.