我的 CVPixelBuffer 为kCVPixelFormatType_32BGRA,我试图获取没有 Alpha 通道的 BGR 格式的帧数据。这是我尝试做的(作为extension CVPixelBuffer)
func bgrData(byteCount: Int) -> Data? {
CVPixelBufferLockBaseAddress(self, .readOnly)
defer { CVPixelBufferUnlockBaseAddress(self, .readOnly) }
guard let sourceData = CVPixelBufferGetBaseAddress(self) else {
return nil
}
let width = CVPixelBufferGetWidth(self)
let height = CVPixelBufferGetHeight(self)
let sourceBytesPerRow = CVPixelBufferGetBytesPerRow(self)
let destinationBytesPerRow = 3 * width
// Assign input image to `sourceBuffer` to convert it.
var sourceBuffer = vImage_Buffer(
data: sourceData,
height: vImagePixelCount(height),
width: vImagePixelCount(width),
rowBytes: sourceBytesPerRow
)
// Make `destinationBuffer` and `destinationData` …Run Code Online (Sandbox Code Playgroud)