如何使用Swift使用kCVPixelFormatType_32BGRA格式的缓冲区创建UIImage

时间:2018-06-25 10:00:23

标签: ios swift cgcontext

我们正在使用以下代码从缓冲区创建UIImage

let context = CGContext(data: baseAddress, width: frameWidth, height: frameHeight, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!

    let cgImage0 =   context.makeImage()  
    let uiImage0 =  UIImage(cgImage: cgImage0!) 

问题是我们只能使用kCapsVixelFormatType_32BGRA格式(仅与kCVPixelFormatType_32RGBA格式最相似)使用AVCaptureVideoDataOutput()创建彩色图像缓冲区。

在某些时候,我们需要UIImage,并且需要将缓冲区转换为UIImage,并且由于UIImage是RGBA格式的颜色已经变形了。

如何从kCVPixelFormatType_32RGBA格式的缓冲区中获取UIImage。

1 个答案:

答案 0 :(得分:1)

通过

   var bitmapInfo  = CGBitmapInfo.byteOrder32Little.rawValue
    bitmapInfo |= CGImageAlphaInfo.premultipliedFirst.rawValue & CGBitmapInfo.alphaInfoMask.rawValue

代替

   var bitmapInfo  = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)

但是很难从Apple文档中获得此信息。