I have a lot of different sized images that I want to display inside of a UIScrollView. I always want the images to be displayed at 300px wide with a variable height (to keep the aspect ratio).

I tried a bunch of things, but nothing works. I either get the images cut off on the sides or squished.

Any ideas?

Any code or examples greatly appreciated!

ios - How do I make my UIImageView automatically scale while having a fixed width?--
ios, uiimageview,

There are some built-in functions in iOS that allow you to look at image data (take a look at . For this instance, if you have a

UIImage *imageObj = [UIImage imageNamed:@"whatever.png"];

You can find the height and/or width CGGetImageWidth/CGGetImageHeight:

size_t pixelsWide = CGImageGetWidth(imageObj.CGImage);
size_t pixelsHigh = CGImageGetHeight(imageObj.CGImage);

UIImage* image = [UIImage imageNamed: @"img.jpg"];

... where image is, let's say 1280 * 720px

300/1280 = 0.234375

This is our scale factor that we'll use and multiply the height by, to match your target width of 300px.

0.234375 * 720 = 168.75px height

Thus, our 1280 * 720px image goes to 300 * 169px. (rounded)



About

pix

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec libero. Suspendisse bibendum. Cras id urna. Morbi tincidunt, orci ac convallis aliquam, lectus turpis varius.

llorem, eu posuere nunc justo tempus leo. Donec mattis, purus nec placerat bibendum, dui pede condimentum odio, ac blandit ante orci ut diam.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec libero. Suspendisse bibendum. Cras id urna.

Lorem Ipsum

Lorem ipsum dolor
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec libero. Suspendisse bibendum. Cras id urna. Morbi tincidunt, orci ac convallis aliquam, lectus turpis varius lorem, eu posuere nunc justo tempus leo. Donec mattis, purus nec placerat bibendum, dui pede condimentum odio, ac blandit ante orci ut diam.

Image Gallery

ad ad ad ad ad ad

Contact Info

Phone: +1234567
Address: 123 TemplateAccess Rd
E-mail: info@example.com