Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The simplest way to resize an UIImage?

In my iPhone app, I take a picture with the camera, then I want to resize it to 290*390 pixels. I was using this method to resize the image :

UIImage *newImage = [image _imageScaledToSize:CGSizeMake(290, 390)                          interpolationQuality:1];     

It works perfectly, but it's an undocumented function, so I can't use it anymore with iPhone OS4.

So... what is the simplest way to resize an UIImage ?

like image 459
pimpampoum Avatar asked Apr 17 '10 14:04

pimpampoum


People also ask

How do I resize an image to scale?

Step 1: Right-click on the image and select Open. If Preview is not your default image viewer, select Open With followed by Preview instead. Step 2: Select Tools on the menu bar. Step 3: Select Adjust Size on the drop-down menu.

How do I make an image smaller in Xcode?

Assuming that you are referring to the layout in storyboard/IB. To get the native size of the image just select the image and press Command + = on the keyboard. the to re-size it proportionally select the corner and hold down the shift key when you re-size it.


1 Answers

The simplest way is to set the frame of your UIImageView and set the contentMode to one of the resizing options.

Or you can use this utility method, if you actually need to resize an image:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {     //UIGraphicsBeginImageContext(newSize);     // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).     // Pass 1.0 to force exact pixel size.     UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);     [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();         UIGraphicsEndImageContext();     return newImage; } 

Example usage:

#import "MYUtil.h" … UIImage *myIcon = [MYUtil imageWithImage:myUIImageInstance scaledToSize:CGSizeMake(20, 20)]; 
like image 70
Paul Lynch Avatar answered Oct 07 '22 03:10

Paul Lynch