How to create CVPixelBuffer from pixel data with YUVFormat

1

I'm focus to create CVPixelBuffer with bytes data (YUV422), this format is my goal but it doesn't work

var yuv422Array = [UInt16](repeating: 0x0000, count: rows*cols)
yuv422Array[0] = 0x0000
let bytesPerRow = cols * 2
CVPixelBufferCreateWithBytes(kCFAllocatorDefault, cols, rows, 
                             kCVPixelFormatType_422YpCbCr16, &baseAddr, bytesPerRow, 
                             nil, nil, nil, &pixelBuffer)

//...

print(pixelBuffer == nil) // false
print(CIImage(cvPixelBuffer: pixelBuffer) == nil) // true ❌ (CIImage is nil) 😥

I'm also trying to create with BGRA data, it doesn't work too.

var rgbArray = [UInt32](repeating: 0xFF204080, count: rows*cols) //DarkBlue
rgbArray[0] = 0xFFFF0000 // Red
rgbArray[1] = 0xFF00FF00 // Green
rgbArray[2] = 0xFF000001 // Black
let bytesPerRow = cols * 4

CVPixelBufferCreateWithBytes(kCFAllocatorDefault, cols, rows,
kCVPixelFormatType_BGRA32, &baseAddr, bytesPerRow, nil, nil, nil,
&pixelBuffer)

//...

print(pixelBuffer == nil) // false
print(CIImage(cvPixelBuffer: pixelBuffer) == nil) // false, (CIImage is NOT nill) 
                                                  // but the result is not correct.

Expect

Result

Is there an alternative way that I can create CVPixelBuffer

  • with final as YUV422 format
  • Capable to set each component (Y,U,V) for all pixels
ios
swift
asked on Stack Overflow Sep 3, 2020 by Chat Dp • edited Sep 3, 2020 by Chat Dp

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0