3

我正在尝试将图像数组转换为视频文件。在这个过程中,我必须从选定的图像中填充像素缓冲区。这是代码片段:

CVPixelBufferLockBaseAddress(pixelBuffer, 0)

let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer)
let bitmapInfo:CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
print("\npixel buffer width: \(CVPixelBufferGetWidth(pixelBuffer))\n")
print("\nbytes per row: \(CVPixelBufferGetBytesPerRow(pixelBuffer))\n")

let context = CGBitmapContextCreate(
    pixelData,
    Int(image.size.width),
    Int(image.size.height),
    CGImageGetBitsPerComponent(image.CGImage),
    CVPixelBufferGetBytesPerRow(pixelBuffer),
    rgbColorSpace,
    bitmapInfo.rawValue
)
CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage)

CVPixelBufferUnlockBaseAddress(pixelBuffer, 0)

执行这些行后,我在 xcode 中收到以下消息: CGBitmapContextCreate: invalid data bytes/row: should be at least 13056 for 8 integer bits/component, 3 components, kCGImageAlphaPremultipliedFirst.CGContextDrawImage: invalid context 0x0。 调试后我得到以下值:

CVPixelBufferGetWidth(pixelBuffer) // value 480
CVPixelBufferGetBytesPerRow(pixelBuffer) // value 1920

我应该怎么做才能获得有效的数据字节/行?控制台日志中指定的3 个组件是什么?我在stackoverflow中看到了类似的问题,但对我来说没有任何帮助。

4

0 回答 0