2010-02-17 43 views
4

我需要将像素编辑器应用程序的内容保存到.png文件中,但我无法找到完成此操作的最佳方法。像素数据存储在32位RGBA缓冲区中。任何人都可以提出我可以用来完成这个任何好工具吗?将32位RGBA缓冲区保存为.png文件(Cocoa OSX)

编辑: 不幸的是,CGImage和representationUsingType:不支持cocotron,我需要能够针对我的应用程序发布个人电脑,任何人都可以提出第三种方式来完成这项任务?

回答

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]; 

如果您不能使用这些Cocoa方法,检查出libpng

+1

这工作很好,但unfortunatley方法representationUsingType:不支持cocotron,我需要能够目标cocotron以及。你能建议任何其他方式来实现这一目标吗? – Mike2012

+0

@Michael,编辑答案包括一个C库​​,将帮助你。 –

2

从像素数据创建一个CGImage并将其送入CGImageDestination

在发布之前不要忘记finalize the destination。这一步是强制性的,而且很容易忘记。

+0

谢谢您的建议,但似乎CGImageCreate不受clozure支持(这是一种常见的lisp - 可可桥),您是否可以为您的方法和上面列出的方法提供其他替代方案。感谢你的帮助! – Mike2012

相关问题