1

I have an image in an UIImageView. There are grey portions above and below the image because the image size doesn't have the same size as UIImageView. How can I crop or zoom the image in such a way that it just fits to cover the grey area above and below, or left and right?

enter image description here

laser2302
  • 427
  • 2
  • 7
  • 19

2 Answers2

4

you should set imageview's contentmode to UIViewContentMode.ScaleAspectFill and you should set propert clipsToBounds of imageview to true.

by this you will not get that gray space and image will fit in entire imageview.!!

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • 1
    It should be `.ScaleAspectFill`, as he wants to fill the whole area instead of having grey borders. – ChaosCoder May 31 '16 at 20:12
  • 1
    ChaosCoder is right, .ScaleAspectFill worked for me and it covered the grey area. However, I'll upvote and choose your answer as an accepted one because you mentioned the property clipsToBounds which really helped me solve this issue. Only applying AspectFill was not enough since the image was getting out of bounds of UIImageView and setting clipToBounds to true solved this issue. So thank you very much! – laser2302 Jun 01 '16 at 15:00
1

Set the contentMode of the UIImageView to UIViewContentMode.ScaleAspectFill. You can do this programmatically or via Interface Builder.

yourImageView.contentMode = .ScaleAspectFill

See Apple Documentation for more details.

ChaosCoder
  • 3,085
  • 1
  • 22
  • 23
  • I think you should use .ScaleAspectFit – Bing May 31 '16 at 12:22
  • No, `.ScaleAspectFill` is correct. https://stackoverflow.com/questions/4895272/difference-between-uiviewcontentmodescaleaspectfit-and-uiviewcontentmodescaletof – ChaosCoder May 31 '16 at 20:11