我需要更改 Objective-C 中现有图像的分辨率,就像 Apple 的预览应用程序工具->调整大小...->分辨率一样。
请让我知道可能的解决方案。
这是我用过的一个很棒的示例 -http://weblog.scifihifi.com/2005/06/25/how-to-resize-an-nsimage/
从该示例中,您可以将 resizedData 写入文件 - 这将是 tiff 格式的调整大小输出。
UPDATE:
这里是 NSImage 类别的实现,它允许使用指定的 DPI 保存 NSImage 实例:
@interface NSImage (DPIHelper)
- (void) saveAsImageType: (NSBitmapImageFileType) imageType withDPI: (CGFloat) dpiValue atPath: (NSString *) filePath;
@end
@implementation NSImage (DPIHelper)
- (void) saveAsImageType: (NSBitmapImageFileType) imageType withDPI: (CGFloat) dpiValue atPath: (NSString *) filePath
{
NSBitmapImageRep *rep = [[self representations] objectAtIndex: 0];
NSSize pointsSize = rep.size;
NSSize pixelSize = NSMakeSize(rep.pixelsWide, rep.pixelsHigh);
CGFloat currentDPI = ceilf((72.0f * pixelSize.width)/pointsSize.width);
NSLog(@"current DPI %f", currentDPI);
NSSize updatedPointsSize = pointsSize;
updatedPointsSize.width = ceilf((72.0f * pixelSize.width)/dpiValue);
updatedPointsSize.height = ceilf((72.0f * pixelSize.height)/dpiValue);
[rep setSize:updatedPointsSize];
NSData *data = [rep representationUsingType: imageType properties: nil];
[data writeToFile: filePath atomically: NO];
}
@end
你可以这样使用它:
NSImage *theImage2 = [NSImage imageNamed:@"image.jpg"];
[theImage2 saveAsImageType:NSJPEGFileType withDPI: 36.0f atPath: @"/Users/<user-name>/image-updated.jpg"];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)