Search code examples
iosuiimageviewuiimageafnetworking

Resize UIImageView to wrap "aspect fitted" image


We're are downloading images from the Internet and assigning them to an UIImageView object with aspectFit as it's contentMode, something like [myImageView setImageWithURL:aURL].

However, aspect fit will resize the image and center it within the boundaries of the view. In other words when the image's width is more than it's height, then, there will be an space in between the bottom border of the UIImageView and the image itself.

Now, I want to include a label below the image without any type of space in between. How can this be achieved? I mean, we could resize the image beforehand and set the ImageView frame afterwards, but, since the image's source is a URL this would required downloading the image first (A bunch of code that is already implemented by UIImageView categories).

Is there a way to tell UIImageView to "wrap" itself around the resizedImage, or is there a way to achieve this effect through constraints?

Thanks


Solution

  • Constraints should handle this. Make sure that your UIImageView only has constraints on the left & right sides (and also top I guess) and no constraints on the height. For your UILabel, it should have a constraint from its top to the bottom of the UIImageView (and whatever horizontal constraints you want on it).