Linkon Sid
Linkon Sid

Reputation: 531

invalid data bytes/row:CGBitmapContextCreate: CGContextDrawImage: invalid context 0x0

I'm trying to convert array of images into a video file. In the process I have to fill pixel buffer from selected images. Here is the code snippet:

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)

After executing these lines I get the following message in xcode: CGBitmapContextCreate: invalid data bytes/row: should be at least 13056 for 8 integer bits/component, 3 components, kCGImageAlphaPremultipliedFirst.CGContextDrawImage: invalid context 0x0. After debugging I get the following value:

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

What should I do to get valid data bytes/row? What are the 3 components specified in console log? I saw similar questions in stackoverflow but nothing helped in my case.

Upvotes: 3

Views: 812

Answers (0)

Related Questions