无效的数据字节/行:CGBitmapContextCreate:CGContextDrawImage:无效的上下文0x0

时间:2016-10-26 10:35:43

标签: ios swift cgcontextdrawimage cgbitmapcontext cvpixelbuffer

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

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:无效数据字节/行:对于8个整数位/组件,3个组件至少应为13056,kCGImageAlphaPremultipliedFirst.CGContextDrawImage:无效的上下文0x0。 调试后,我得到以下值:

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

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

0 个答案:

没有答案