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

Ami*_*Sri 5 cocoa resolution objective-c nsimage

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

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

Den*_*nis 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
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用它:

NSImage *theImage2 = [NSImage imageNamed:@"image.jpg"];
[theImage2 saveAsImageType:NSJPEGFileType withDPI: 36.0f atPath: @"/Users/<user-name>/image-updated.jpg"];
Run Code Online (Sandbox Code Playgroud)