将32位RGBA缓冲区保存到.png文件中(Cocoa OSX)

Mik*_*012 4 macos cocoa buffer cocotron

我需要将像素编辑器应用程序的内容保存到.png文件中,但我无法找到实现此目的的最佳方法.像素数据存储在32位RGBA缓冲器中.任何人都可以建议我可以使用任何好工具来完成这个任务吗

编辑:不幸的是,CGImage和representationUsingType:不受cocotron支持,我需要能够针对我的PC版本的应用程序中,也有人认为完成这一任务的第三条道路?

Car*_*rum 5

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];
Run Code Online (Sandbox Code Playgroud)

如果你不能使用这些Cocoa方法,请查看libpng.