2010-07-30 77 views
0

我在可可應用程序中將此代碼調整爲某個尺寸的PNG文件,但我希望格式仍然是PNG,但沒有PNGRepresentation方法NSImage中。 我該怎麼做?在可可dev中將圖像從PNG格式調整爲PNG格式

NSData * sourceData = [NSData dataWithContentsOfFile:fileName];

NSImage *sourceImage = [[NSImage alloc] initWithData: sourceData]; 

NSSize originalSize = [sourceImage size]; 
float resizeWidth = originalSize.width - 10; 
float resizeHeight = originalSize.height - 10; 
NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(resizeWidth, resizeHeight)]; 

[resizedImage lockFocus]; 
[sourceImage drawInRect: NSMakeRect(0, 0, resizeWidth, resizeHeight) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0]; 
[resizedImage unlockFocus]; 

NSData *resizedData = [resizedImage TIFFRepresentation]; 
[resizedData writeToFile:@"~/playground/resized.tif" atomically:YES]; 
[sourceImage release]; 
[resizedImage release]; 

回答

0

您需要一個位圖圖像代表來詢問其PNG表示。

將焦點鎖定在調整大小後的圖像上時,請創建和init a bitmap image rep with the focused 「view」,傳遞圖像的邊界(原始NSZeroPoint,矩形大小=圖像大小)。然後問代表its representation as a PNG file

請注意,您將失去諸如DPI,伽馬,顏色配置文件等屬性。如果要保留這些屬性,則需要使用Core Graphics來完成整個作業,從讀取文件(使用CGImageSource)調整圖像大小(使用CGBitmapContext)以寫入新文件(使用CGImageDestination)。