我需要将iOS相机中的原始数据保存到云中。我从iOS相机收到CVPixelBuffer
。设置iOS相机时,我没有指定要使用kCVPixelBufferPixelFormatTypeKey
的格式(CVPixelBuffer
。
我将CVPixelBuffer
变成这样的Data
对象。
let buffer: CVPixelBuffer = //My buffer from the camera
//Get the height for the size calculation
let height = CVPixelBufferGetHeight(buffer)
//Get the bytes per row for the size calculation
let bytesPerRow = CVPixelBufferGetBytesPerRow(buffer)
//Lock the buffer so we can turn it into data
CVPixelBufferLockBaseAddress(buffer, CVPixelBufferLockFlags.readOnly)
//Get the base address. This is the address in memory of where the start of the buffer is currently stored.
guard let pointer = CVPixelBufferGetBaseAddress(buffer) else {
//If we failed to get the base address unlock the buffer and clean up.
CVPixelBufferUnlockBaseAddress(buffer, CVPixelBufferLockFlags.readOnly)
return
}
let data = Data(bytes: pointer, count: height * bytesPerRow)
我这样做正确吗?
此外,当我取回这些数据时,如何将其变成CIImage
?