如何在objective-c中更改图像分辨率

时间:2011-11-08 09:55:44

标签: objective-c cocoa resolution nsimage

我需要更改objective-c中现有图像的分辨率,就像Apple的预览应用程序工具 - >调整大小...->分辨率一样。

请告诉我可能的解决方案。

3 个答案:

答案 0 :(得分:16)

这是我用过的一个很棒的例子 - http://weblog.scifihifi.com/2005/06/25/how-to-resize-an-nsimage/

从该示例中,您可以将resizedData写入文件 - 这将是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"];

答案 1 :(得分:0)

请参阅苹果开发者website ImageApp
ImproveYourImage

答案 2 :(得分:0)

您无法将图像转换为高分辨率。那么你想压缩你的形象还是什么?

如果它是您想要的,那么您可以使用: UIImagePNGRepresentation 和/或 UIImageJPEGRepresentation 。需要设置质量属性的位置。根据质量,您的图像尺寸将减少。