我需要在Objective-C中像Apple的预览应用程序工具->调整大小...->分辨率中一样更改现有图像的分辨率。
请告诉我可能的解决方案。
我需要在Objective-C中像Apple的预览应用程序工具->调整大小...->分辨率中一样更改现有图像的分辨率。
请告诉我可能的解决方案。
我用过的一个很棒的示例是 - http://weblog.scifihifi.com/2005/06/25/how-to-resize-an-nsimage/
从那个示例中,你可以将调整大小后的数据写入文件 - 这将是 tiff 格式的调整大小输出。
更新:
这里是 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"];
您不能将图像转换为高分辨率。那么您想压缩图像吗?
如果这是您想要的,那么您可以使用:UIImagePNGRepresentation和/或UIImageJPEGRepresentation。需要设置quality属性。根据质量,您的图像大小将会减小。