2

I have a view where I need to place an UIImageView, and I was told to place it inside a rectangle that takes the screen width and its height is smaller than width (fixed size). However, the images I've been given are more or less square jpeg images, so the idea is to fill the width of the rectangle that should contain the image and set the height of the image in a way that its aspect ratio is kept. Then, I should be able to let the user scroll the image vertically to see the complete image.

How could I do this?

Thanks in advance

EDIT: I need to do this for several images that have different sizes, but that should fit the same rectangular area size within the view

1 Answer 1

8

You can set imageview content mode.

imageView.contentMode = UIViewContentModeScaleAspectFit;

This will make sure that the image is displayed by keeping original aspect ratio.

Edit:

I think this is what you wanted:

UIImage *originalImage = [UIImage imageNamed:[picArr objectAtIndex:a]];
double width = originalImage.size.width;
double height = originalImage.size.height;
double apect = width/height;

double nHeight = 320.f/ apect;
self.img.frame = CGRectMake(0, 0, 320, nHeight);
self.img.center = self.view.center;
self.img.image = originalImage;

Hope this helps.. :)

1
  • Thanks, but this way what I get is an image that does not fit the screen width, since it adjusts the height to the rectangle where I place the image within...
    – AppsDev
    Commented Aug 21, 2014 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.