我需要将像素编辑器应用程序的内容保存到 .png 文件中,但我无法找到完成此操作的最佳方法。像素数据存储在 32 位 RGBA 缓冲区中。谁能建议我可以用来完成此任务的任何好工具?
编辑:不幸的是,cocotron 不支持 CGImage 和 representationUsingType: ,我还需要能够将我的应用程序定位为 PC 版本,有人可以建议第三种方法来完成这项任务吗?
NSBitmapImageRep
应该得到你所需要的。将数据加载到 中NSBitmapImageRep
,然后用于representationUsingType:properties:
将其作为 PNG 导出。一个简单的例子:
NSBitmapImageRep *imageRep =
[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:imageBuffer
pixelsWide:imageWidth
pixelsHigh:imageHeight
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:imageWidth * 4
bitsPerPixel:32];
NSData *pngData = [imageRep representationUsingType:NSPNGFileType
properties:propertyDictionary];
如果您不能使用这些 Cocoa 方法,请查看libpng
.
从像素数据创建CGImage并将其提供给CGImageDestination。
不要忘记在发布之前确定目的地。这一步是强制性的,而且很容易忘记。