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.
Asked
Active
Viewed 369 times
0

Jay Bhalani
- 4,142
- 8
- 37
- 50

iOS_Learner
- 159
- 9
-
It is not possible. UIImage will be stretched according to size of UIImageView. – iPhone May 13 '15 at 06:21
-
@ iPhone any tricky solution , which cold help me ? – iOS_Learner May 13 '15 at 06:24
-
But why do you want such kind of solution? – iPhone May 13 '15 at 06:25
-
I think you should keep the size of UIImageView fixed. No other solution as per my sight – iPhone May 13 '15 at 06:30
-
please display your problem in screen shot @Salman – Jay Bhalani May 13 '15 at 06:35
-
@JayBhalani wait plz – iOS_Learner May 13 '15 at 06:40
-
@Salman your screenshot is not clear. i am not getting what you want exactly form your screen shot. – Ashok Londhe May 13 '15 at 07:03
-
@JayBhalani please check the screenshot.... my device is iPad 3 and image size was 480 X 360 but my image stretched according to the UIImageView. I don't want this. Image should not be stretched no matter how much space would be empty around the Image. – iOS_Learner May 13 '15 at 07:05
-
@Salman please put your device screen shot. – Jay Bhalani May 13 '15 at 07:20
2 Answers
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
-
-
@Salman i have tried it but works.. you can see the link here http://stackoverflow.com/questions/7035523/how-to-make-image-not-stretching-in-uiimageview – Ashok Londhe May 13 '15 at 06:53
-
@Salman Please share screen shot of your output and expected output. – Ashok Londhe May 13 '15 at 06:54
-
@AshokLondhe I have posted the screenshot. My Image size is 480 X 360 , my UIImageView resized according to iPad , but I don't want get my image resize or stretched. – iOS_Learner May 13 '15 at 07:01
-
@Salman have you seen the link which i have mentioned in previous comment. – Ashok Londhe May 13 '15 at 07:04
-
-
@AshokLondhe I have seen the link you posted, and tried the "Centre"content mode for UIImageView. It sets my Image in the centre of the UIImageView and doesn't stretches the Image. – iOS_Learner May 13 '15 at 07:19
-
@AshokLondhe tell me how can I stretch my Image according to its width within the UIImageView ? – iOS_Learner May 13 '15 at 07:21
-
@Salman then no need to write above lines of code. remove it and try. – Ashok Londhe May 13 '15 at 07:23
-
@AshokLondhe if Image is landscape it should strectched width wise and if the image is potrait then it should stretched height wise. this is what I want to achieve. Keep it in mind that Image size would be 480 X 360. – iOS_Learner May 13 '15 at 07:25
-
@Salman try to add constraint to image view.. that is vertical and horizontal. – Ashok Londhe May 13 '15 at 07:30
-
@AshokLondhe you mean do i need to use auto layout constaraints or need to do something programatically – iOS_Learner May 13 '15 at 07:33
-
-
@Salman i have added .. vertical space constraint -top guide and horizontal space. and it works fine. you can do and check it. – Ashok Londhe May 13 '15 at 07:39
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