5

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.

AmitSri
  • 1,209
  • 1
  • 20
  • 48

3 Answers3

17

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"];
Denis
  • 6,313
  • 1
  • 28
  • 27
  • Hi, Thanks for the link, but i am looking code for changing image resolution (DPI/PPI) to 72, 96, 100, 120, 150 and soon..., not the image physical size or create its thumbnail. Please let me know if you know some help regarding changing an image DPI/PPI resolution. – AmitSri Nov 17 '11 at 12:10
  • this thread is discussing DPI conversion issue http://www.cocoadev.com/index.pl?ImageDPI - they have solved this accessing NSBitmapImageRep. Good luck! – Denis Nov 17 '11 at 13:10
  • Hi, thanks again taking time to provide me reference. But, again my question remains unanswered. I need a simple code which will change NSImage/NSBitmapImageRep DPI to given one. I don't get how i can change the DPI by composing existing NSImage to new one. Please provide me simple NSImage category or method which will take NSImage and new DPI as argument and return the new NSImage with passed DPI. On saving that new NSImage must be equivalent to Preview app manually adjusted image. – AmitSri Nov 18 '11 at 10:34
  • AmitSri, I've updated my answer with a NSImage category implementation, that does what you're asking for – Denis Nov 18 '11 at 14:01
  • Denis, Thanks your given code seems to be working with little issue. I am first loading 300 DPI image and changing it to 72 DPI which is working fine. Now, when i again take same 72 DPI converted image and trying to change it to back 300 DPI, then i am getting DPI Height = 300 and DPI width = 299.7073 in Preview APP. Please let me know is this a common behavior or just rounding issue in Preview App. Thanks, again for the help. – AmitSri Nov 19 '11 at 07:56
  • yep, looks like a rounding issue - i'm using ceil, that just takes the smallest integer, maybe it will be better for using roundf or something; – Denis Nov 19 '11 at 08:11
  • Hi, thanks i have used roundf() instead of ceilf() and it seems to be little better now. – AmitSri Nov 19 '11 at 08:28
  • Just wondering how can Preview App setting DPI properly? Just tried same thing via preview App and it is doing correctly. – AmitSri Nov 19 '11 at 10:16
  • Is there some way to just set the DPI information of a JPG file without change its width and height on IOS? The code provided by Denis above uses NSBitmapImageRep and in IOS we can't find this class. We have a JPG with 2652 x 1850 with 72 dpi and want to create a JPG with the same size (2652x1850) with 300 dpi (just to have a print size more reasonable). Any suggestions will be welcome. Important : we need this on a IPAD/IPHONE application – Robson Reis Jan 23 '14 at 14:24
0

Refer apple developer website .
ImageApp.
ImproveYourImage

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • Hi, Thanks i have already checked those apps, but didn't find any code which will help me to adjust the image resolution directly on NSImage and save it as new or overwrite it on existing one. let me know, if i a missing something. – AmitSri Nov 08 '11 at 10:23
0

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.

Mrunal
  • 13,982
  • 6
  • 52
  • 96