【发布时间】:2019-05-17 12:19:21
【问题描述】:
通过使用 CGContext 和下面的代码,我们可以从缓冲区创建图像。 在这段代码中,您基本上指向内存中的起始地址,以及您将从内存中的该点获取数据以构造图像的距离。
但是,如果我们有一个变量来保存图像数据,让我们说 imageData 作为我们从其他来源(例如本地网络中的其他设备)下载的数据类型。
let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer)
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer)
let colorSpace = CGColorSpaceCreateDeviceRGB()
var bitmapInfo = CGBitmapInfo.byteOrder32Little.rawValue
bitmapInfo |= CGImageAlphaInfo.premultipliedFirst.rawValue
//CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
let context = CGContext(data: baseAddress, width: videoWidth, height: videoHeight, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo)!
let frameImage = context.makeImage()! // This is a CGImage
【问题讨论】:
-
你看过
CGDataProvider吗?