【发布时间】:2010-07-30 16:09:45
【问题描述】:
我在可可应用程序中有这段代码,可以将 PNG 文件调整到某个尺寸,但我希望格式仍然是 PNG,但是 NSImage 没有 PNGRepresentation 方法。 我该怎么做?
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];
【问题讨论】: