0

This is heavily related to my question here: Is there a way to get a high-res YouTube video thumbnail without letter-boxing?

Say I always get images back from an API that need to be cropped. They always have black letter-boxing at the top and bottom.

Now I could programmatically crop every UIImage down to the size it should be, but actually altering the image seems like a very expensive operation, and a waste when I really just need to hide it.

How would I best go about putting it in a UIImageView and making sure that the black bars aren't visible? To make it easier, it's safe to assume that it will be cropped into an aspect ratio that is the same as what the image is without the black bars (so in the above example, 16:9).

Community
  • 1
  • 1
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • If I right understood you you can place UIView upper by UIImageView with frame you need and set up view.clipsToBounds=YES; – stosha Oct 23 '14 at 03:31
  • Size the image view to the right ratio and then `clipToBounds` to `YES` and set `contentMode` of `UIViewContentModeScaleAspectFill`. – Rob Oct 23 '14 at 04:02
  • If all the images are the same size to begin with I would use blank white UIViews to form a frame. In Storyboard put a UIImageView in the center of your VCs view and size it to the size of the incoming images. Place a sample image like what you will be downloading on the UIImageView. Use four blank UIViews on top to mask out the black bars (or two if that's all it takes.) In your code you can easily swap out the UIImage in the ImageView without disturbing the layering. This would be simple. The purists around here wont like it. Maybe they'll have a more complicated way to accomplish this. – Tim Quinn Oct 23 '14 at 06:14

1 Answers1

0

Resample or Crop the image accordingly and then

YourImageview.contentMode = UIViewContentModeScaleAspectFit // or you can use UIViewContentModeScaleToFill;

then...

YourImageView.layer.maskstobounds = yes; // or YourImageView. clipToBounds = yes;
Aritra Das
  • 416
  • 3
  • 4