Ranjit
Ranjit

Reputation: 4646

Resize image and maintain its aspect ratio in iOS

I am trying to select an image from photo library, this is the photo enter image description here which I have downloaded from net and is stored in my photo lib.

So now I want to give user an option to select an image from photo lib and apply as a backgroundImage throught my app, but when I do it, I get the image this way on my iphone screen

enter image description here

Actually the image has been filled the screen, because the content mode specified is "scaleToFill".

1) I want to know how to make this image maintain its aspect ratio and also fill the screen?

Regards Ranjit

Upvotes: 1

Views: 1562

Answers (2)

NSLog
NSLog

Reputation: 649

check this blog. use these two files

UIImage+Resize.h

UIImage+Resize.m

UIImage *image = [UIImage imageNamed:@"SomeImage-900x675"]; // SomeImage-900x675.png 
CGFloat targetWidth = 320.0;
CGFloat scaleFactor = targetWidth / image.size.width; 
CGFloat targetHeight = image.size.height * scaleFactor; 
CGSize targetSize = CGSizeMake(targetWidth, targetHeight);
UIImage *scaledImage = [image resizedImage:targetSize interpolationQuality:kCGInterpolationHigh];

Upvotes: 2

swapnali patil
swapnali patil

Reputation: 304

though your image is taking all the screen size, Your image size is biger...You will try by scaling image with following resolutions 320*480, 640*960, 1240*2208

Upvotes: 0

Related Questions