Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize the image programmatically in objective-c in iphone

I have an application where I am displaying large images in a small space. The images are quite large, but I am only displaying them in 100x100 pixel frames. My app is responding slowly because of the size fo the images I am using.

To improve performance, how can I resize the images programmatically using Objective-C?

like image 487
Madan Mohan Avatar asked Jan 17 '11 10:01

Madan Mohan


People also ask

How do I resize an image in Xcode?

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.

How do I change the size of my Uiimageview?

Here is a simple way: UIImage * image = [UIImage imageNamed:@"image"]; CGSize sacleSize = CGSizeMake(10, 10); UIGraphicsBeginImageContextWithOptions(sacleSize, NO, 0.0); [image drawInRect:CGRectMake(0, 0, sacleSize. width, sacleSize.


2 Answers

Please find the following code.

- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size {     UIGraphicsBeginImageContext(size);     [image drawInRect:CGRectMake(0, 0, size.width, size.height)];     UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();         UIGraphicsEndImageContext();     return destImage; } 
like image 134
Satya Avatar answered Sep 18 '22 09:09

Satya


This code is for just change image scale not for resizing. You have to set CGSize as your image width and hight so the image will not stretch and it arrange at the middle.

- (UIImage *)imageWithImage:(UIImage *)image scaledToFillSize:(CGSize)size {     CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height);     CGFloat width = image.size.width * scale;     CGFloat height = image.size.height * scale;     CGRect imageRect = CGRectMake((size.width - width)/2.0f,                                   (size.height - height)/2.0f,                                   width,                                   height);      UIGraphicsBeginImageContextWithOptions(size, NO, 0);     [image drawInRect:imageRect];     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();     return newImage; } 
like image 27
Kaushik Movaliya Avatar answered Sep 19 '22 09:09

Kaushik Movaliya