如何改变目标c中的图像分辨率 [英] how to change image resolution in objective-c

查看:190
本文介绍了如何改变目标c中的图像分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



请让我知道可能的解决方案。

解决方案

这里有一个很好的例子我使用 - http://weblog.scifihifi.com/2005/06/25/how-to-resize-an-nsimage/



从该示例中,您可以将resizedData写入文件 - 这将是tiff格式的调整大小的输出。



UPDATE:



这里是NSImage类别实现,允许使用指定的DPI保存NSImage实例:


$ (NSBitmapImageFileType)imageType withDPI:(CGFloat)dpiValue atPath:(NSString *)filePath;(b)
@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];


I need to change the resolution of the existing image in objective-c just like Apple's Preview application Tools->Adjust Size...->Resolution.

Please let me know the possible solutions.

解决方案

Here's a great sample I've used - http://weblog.scifihifi.com/2005/06/25/how-to-resize-an-nsimage/

From that sample, you can write resizedData to the file - and this will be a resized output in tiff format.

UPDATE:

here comes the NSImage category implementation, that allows to save NSImage instance with specified DPI:

@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

you can use it like this:

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

这篇关于如何改变目标c中的图像分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆