将32位RGBA缓冲区保存到.png文件(Cocoa OSX) [英] Saving a 32 bit RGBA buffer into a .png file (Cocoa OSX)
问题描述
我需要将像素编辑器应用程序的内容保存为.png文件,但我无法找到实现此目的的最佳方法。像素数据存储在32位RGBA缓冲器中。任何人都可以建议任何好的工具,我可以用来完成这个?
I need to save the contents of a pixel editor application into a .png file but I am having trouble finding the best way to accomplish this. The pixel data is stored in a 32 bit RGBA buffer. Can anyone suggest any good tools I could use to accomplish this?
编辑:
不幸的是,CGImage和representationUsingType:不支持cocotron,我需要能够定位我的应用程序的PC版本,建议第三种方式完成这项任务?
Unfortunately, CGImage and representationUsingType: are not supported by cocotron and I need to be able to target my app for PC release as well, can anyone suggest a third way of accomplishing this task?
推荐答案
NSBitmapImageRep
应该会得到你所需要的。将数据加载到 NSBitmapImageRep
中,然后使用 representationUsingType:properties:
PNG。一个快速示例:
NSBitmapImageRep
should get you what you need. Load the data up into the NSBitmapImageRep
and then use representationUsingType:properties:
to get it out as a PNG. A quick example:
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
。
If you can't use these Cocoa methods, check out libpng
.
这篇关于将32位RGBA缓冲区保存到.png文件(Cocoa OSX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!