2

I have an Image and an ImageView, the ImageView occupies the whole screen and I want to resize the image to fit the ImageView. The ImageView is inside a ScrollView with only vertical scroll bar enabled, which means the Image should be resized with maximum width equal to that of the screen(ImageView), in the mean time keeping the ratio, not constrained on the resized height. The ImageView's height will fit with resized Image, if the height exceeds the height of the screen we can use the scroll bar to see it.

So I want suggestions on:

1) How I can resize an Image with a fixed width if the original width is less than the desired one, no resize is required. No constraint on height.

2) How I can make the ImageView fit the height of the Image automatically?

ZhangChn
  • 3,154
  • 21
  • 41
Bin Chen
  • 61,507
  • 53
  • 142
  • 183

1 Answers1

0

First, you can calculate the new image's height by yourself.

(if, image's size = (300,200), imageView's width = 320, the new image's height = 200*(320/300))

Then, from this thread The simplest way to resize an UIImage?

You can resize the Image with new width and height.


Total soultion:

UIImage* i= /* original image */;
UIImage* j =[self imageWithImage:i scaledToSize:newSize]; //new Image
[theImageView setImage:j];

(And this code also works fine with iOS 4.x)

Community
  • 1
  • 1
user9527
  • 315
  • 2
  • 11