how to change image resolution in objective-c

China☆狼群 提交于 2019-12-18 12:37:15

问题


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.


回答1:


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"];



回答2:


Refer apple developer website .
ImageApp.
ImproveYourImage




回答3:


You can not turn your image out to high resolution. So do you want to compress your image or what?

If it is what you want then you can use: UIImagePNGRepresentation and/or UIImageJPEGRepresentation. Where it is required to set the quality property. According to quality your image size will get reduced.



来源:https://stackoverflow.com/questions/8048597/how-to-change-image-resolution-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!