views:

5363

answers:

5

My application is downloading a set of image files from the network, and saving them to the local iPhone disk. Some of those images are pretty big in size (widths larger than 500 pixels, for instance). Since the iPhone doesn't even have a big enough display to show the image in its original size, I'm planning on resizing the image to something a bit smaller to save on space/performance.

Also, some of those images are JPEGs and they are not saved as the usual 60% quality setting.

How can I resize a picture with the iPhone SDK, and how can I change the quality setting of a JPEG image?

+9  A: 

A couple of suggestions are provided as answers to this question. I had suggested the technique described in this post, with the relevant code:

+ (UIImage*)imageWithImage:(UIImage*)image 
               scaledToSize:(CGSize)newSize;
{
   UIGraphicsBeginImageContext( newSize );
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return newImage;
}

As far as storage of the image, the fastest image format to use with the iPhone is PNG, because it has optimizations for that format. However, if you want to store these images as JPEGs, you can take your UIImage and do the following:

NSData *dataForJPEGFile = UIImageJPEGRepresentation(theImage, 0.6);

This creates an NSData instance containing the raw bytes for a JPEG image at a 60% quality setting. The contents of that NSData instance can then be written to disk or cached in memory.

Brad Larson
+2  A: 

The easiest and most straightforward way to resize your images would be this

float actualHeight = image.size.height;
float actualWidth = image.size.width;
float imgRatio = actualWidth/actualHeight;
float maxRatio = 320.0/480.0;

if(imgRatio!=maxRatio){
 if(imgRatio < maxRatio){
  imgRatio = 480.0 / actualHeight;
  actualWidth = imgRatio * actualWidth;
  actualHeight = 480.0;
 }
 else{
  imgRatio = 320.0 / actualWidth;
  actualHeight = imgRatio * actualHeight;
  actualWidth = 320.0;
 }
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lostInTransit
+1  A: 

I would strongly recommend resizing the images server side with ImageMagik. Downloading large images and resizing them on the phone is a waste of many precious resources - bandwidth, battery and memory. All of which are scarce on phones.

Roger Nolan
A: 

Hi Brad

Nice Solution,

Just so you know I am very new to xcode so I was really hard to know how to call this function.

Finally I figured it out

cell.image = [self imageWithImage:image scaledToSize:CGSizeMake(32.0f, 32.0f)];

Aan It works forme, thanks for your help

A: 

Mixing Brad's and lostInTransit's answers, it was possible to create a function which allows me to create an image with a maximum size, and keeping the ratio between width and height.

I was an agnostic, but no I'm not. You guys are Gods! ;)

(either that or I got a new proof that I'm still a newbie at this).

camilo