2010-02-17 42 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

相關問題