我正在尝试让CIImage解压缩数据。目前,我发现获得压缩数据的唯一方法是使用CIContext,如下所示:
let ciContext = CIContext()
let ciImage = CIImage(color: .red).cropped(to: .init(x: 0, y: 0, width: 192, height: 192))
guard let ciImageData = ciContext.jpegRepresentation(of: ciImage, colorSpace: CGColorSpace(name: CGColorSpace.sRGB)!, options: [:]) else {
fatalError()
}
print(ciImageData.count) // Prints 1331是否有可能(尽可能有效地)获得未压缩的CIImage数据?
发布于 2020-08-23 11:54:22
如您所见,ciContext.jpegRepresentation正在将图像数据压缩为JPEG,并为您提供一个Data对象,该对象可以作为- is文件写入磁盘(包括图像元数据)。
您需要使用不同的CIContext API直接呈现(未压缩的)位图数据:
let rowBytes = 4 * Int(ciImage.extent.width) // 4 channels (RGBA) of 8-bit data
let dataSize = rowBytes * Int(ciImage.extent.height)
var data = Data(count: dataSize)
data.withUnsafeMutableBytes { data in
ciContext.render(ciImage, toBitmap: data, rowBytes: rowBytes, bounds: ciImage.extent, format: .RGBA8, colorSpace: CGColorSpace(name: CGColorSpace.sRGB)!)
}或者,您可以创建一个具有正确大小和格式的CVPixelBuffer,并使用CIContext.render(_ image: CIImage, to buffer: CVPixelBuffer)将其呈现为该格式。我认为Core直接支持CVPixelBuffer输入,所以这可能是更好的选择。
https://stackoverflow.com/questions/63536340
复制相似问题