44

I have an UIImageView with some fixed rect size. But my images are not fixed in size. I want to display images according to UIImageView's rect size. Whether image is big or large in resolution it must be in fixed display according to UIImageView's size. I am confused in using below assets.

UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw

What to set in autoresize mask ? How do they behave ?

Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90
DipakSonara
  • 2,598
  • 3
  • 29
  • 34
  • Why don't you check by using the above mentioned values ? You can find the difference easily – Midhun MP Jan 03 '13 at 06:32
  • 2
    See this link : http://stackoverflow.com/questions/4895272/difference-between-uiviewcontentmodescaleaspectfit-and-uiviewcontentmodescaletof – Anusha Kottiyal Jan 03 '13 at 06:49
  • UIViewContentModeScaleAspectFill use tho property of UIImageView content mode. It will fit your image to UIIMageView rect size. – fibnochi Jan 03 '13 at 07:03

7 Answers7

90

Please try this code, Hope it will work for you.

set UIImageView contentMode to UIViewContentModeScaleAspectFill as below :

imageView.contentMode = UIViewContentModeScaleAspectFill;

set autoresizingMask of UIImageView as below :

imageView.autoresizingMask =   
    ( UIViewAutoresizingFlexibleBottomMargin
    | UIViewAutoresizingFlexibleHeight
    | UIViewAutoresizingFlexibleLeftMargin
    | UIViewAutoresizingFlexibleRightMargin
    | UIViewAutoresizingFlexibleTopMargin
    | UIViewAutoresizingFlexibleWidth );
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
sagarcool89
  • 1,366
  • 8
  • 16
3

You just need these two lines:

imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.layer.clipsToBounds = YES; // Must do this or else image will overflow
atulkhatri
  • 10,896
  • 3
  • 53
  • 89
  • 2
    Thank you! `ClipsToBounds` was my issue. I had it set to `NO` so I could display a shadow around the UIImageView, without thinking about the fact that it'll also let the image bleed over. Thanks for the tip. – StoriKnow Sep 16 '18 at 13:52
2

Here a solution for swift:

let imageView = UIImageView(image: UIImage(named: "backgroundImage"))
imageView.frame = tableView.frame
imageView.contentMode = .ScaleAspectFill
tableView.backgroundView = imageView
Urkman
  • 1,298
  • 1
  • 16
  • 32
1

If you want to make your UIImageView size according to ratio ,then you must set it's mode to UIViewContentModeScaleAspectFit

 yourimage.contentMode=UIViewContentModeScaleAspectFit;

let me know it is working or not!!!

Happy Coding.

DD_
  • 7,230
  • 11
  • 38
  • 59
NiravPatel
  • 3,260
  • 2
  • 21
  • 31
  • No, its not working. Image isn't covering whole area of UIImageView. – DipakSonara Jan 03 '13 at 06:42
  • yes, it will not cover the whole area of UIImageview because it will set your image according to ratio, so it will left some area blank according to ratio.so you will get image without blur and get clear image.so do you want to cover the whole area of UIImageview? – NiravPatel Jan 03 '13 at 06:47
1

In swift you set content mode as well

var newImgThumb : UIImageView
newImgThumb = UIImageView(frame:CGRectMake(0, 0, 100, 70))
newImgThumb.contentMode = .ScaleAspectFit
Ali Raza
  • 2,796
  • 1
  • 20
  • 24
1

Worth noting that swift 3 has changed as following

.ScaleAspectFit to scaleAspectFit

.ScaleAspectFill to .scaleAspectFill

karthikeyan
  • 3,821
  • 3
  • 22
  • 45
kickinbahk
  • 13
  • 4
0

Hope this helpful for you

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);

image = [UIImage imageWithCGImage:imageRef]];

CGImageRelease(imageRef);
SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39