Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing image to fit UIImageView

Tags:

I am very new to objective c and I'm just getting my bearings. I want to do something really simple but it proves to be quite a challenge:

I am trying to display an image into an UIImageView. The image I'm showing is large and I want it scaled down to fit the UIImageView. I tried setting the AspectFit View mode but the image gets displayed to the original size and is clipped by the UIImageView. My code is below:

- (void)changeImages {     UIImage* img11 = nil;      img11 = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"jpeg"]];      u11.contentMode = UIViewContentModeScaleAspectFit;     u11.image = img11; }  - (void)viewDidLoad {     [super viewDidLoad];     // Do any additional setup after loading the view, typically from a nib.      [self changeImages]; } 

Can anyone shed some light on this please?

Thanks!

like image 499
cristi71000 Avatar asked Sep 23 '12 13:09

cristi71000


People also ask

How do I change the size of my UIImageView?

The simplest way is to set the frame of your UIImageView and set the contentMode to one of the resizing options. I would NOT retain the result here. newImage will be autoreleased already, and it should up to the method that called this to retain it or not.

How do I change aspect ratio in UIImageView?

To maintain aspect ratio I calculated the width of UIImageView = (320/460)*450 = 313.043 dynamically. And set the contentMode For UIImageView is UIViewContentModeScaleAspectFit. And set the image(320x460) to image view but it is some what blur.

How do you adjust the way an image is fitted to its space Swift?

To make an image scales to fit the current view, we use the resizable() modifier, which resizes an image to fit available space. We can see the whole image now, but it got stretched out and losing its aspect ratio, which is usually not the behavior we want. The image view resize to fit available space.

Does UIImageView have intrinsic content size?

Here is the essential problem: the only way in which UIImageView interacts with Auto Layout is via its intrinsicContentSize property. That property provides the intrinsic size of the image itself, and nothing more.


2 Answers

Hi I would try this...

- (void)changeImages {     UIImage *img11 = [UIImage imageNamed@"dog.jpeg"];      u11.contentMode = UIViewContentModeScaleAspectFit;     u11.clipsToBounds = YES;     [u11 setImage:img11]; }  - (void)viewWillAppear:animated {     [super viewWillAppear:animated];      [self changeImages]; } 

This will scale the image (up or down) so that it fits inside the imageView. Having clipsToBounds isn't necessary but will stop the image from displaying outside the frame of your imageView.

HTH.

like image 103
Fogmeister Avatar answered Oct 05 '22 02:10

Fogmeister


Add to your UIViewController.m:

-(UIImage *)resizeImage:(UIImage *)image imageSize:(CGSize)size {     UIGraphicsBeginImageContext(size);     [image drawInRect:CGRectMake(0,0,size.width,size.height)];     UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();     // here is the scaled image which has been changed to the size specified     UIGraphicsEndImageContext();     return newImage; } 

Using:

UIImage *image = [UIImage imageNamed:@"image.png"]; CGSize size = CGSizeMake(50, 63); // set the width and height  UIImage *resizedImage = [self resizeImage:image imageSize:size]; 

I hope it helps.

like image 43
Erhan Demirci Avatar answered Oct 05 '22 02:10

Erhan Demirci