0

Image size is 480X360

I am using the UIImageView in my xib. App is designed for multi device without auto layout. It means I am using autoresizing.What I want to do is, only my UIImageView should autoresize not my Image inside the UIImageView, but unfortunately my image also getting stretched with UIImageView. I have tried the different ways but could not get success. Changed the content mode also but didn't work for me.

Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
iOS_Learner
  • 159
  • 9

2 Answers2

0

Try this...

//set content mode
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
//clear background color
self.imageView.backgroundColor=[UIColor clearColor];

UIViewContentMode

UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
Britto Thomas
  • 2,092
  • 1
  • 15
  • 28
Ashok Londhe
  • 1,491
  • 11
  • 29
0

Don't add the UIImage to the UIImageView whose size is going to change. Add it to another UIImageView and make this second UIImageView a subview of the first UIImageView.

UIImage *exampleImage = [UIImage imageWithContentsOfFile:filePath];
UIImageView *variableImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

CGFloat imageX = (variableImageView.bounds.size.width - exampleImage.size.width) / 2;
CGFLoat imageY = (variableImageView.bounds.size.height - exampleImage.size.height) / 2;

UIImageView *helperImageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, exampleImage.size.width, exampleImage.size.height)];

helperImageView.image = exampleImage;
helperImageView.contentMode = UIViewContentModeScaleAspectFit;

[self.view addSubview:variableImageView];
[variableImageView addSubview:helperImageView];

I hope this helps.

enigma
  • 865
  • 9
  • 22