iosuiimageviewcontentmode

image doesn't fit in UIImageView


I am a beginner in iOS development and stuck at the following problem.

In .xib file I have button and imageview as displayed in following image.

enter image description here

Now I did the following to show image in image view and set content mode in .m file.

UIImage *save = UIGraphicsGetImageFromCurrentImageContext();
self.ivCard.image = save;
self.ivCard.contentMode = UIViewContentModeScaleAspectFit;
self.ivCard.clipsToBounds = YES;

Now the result looks like the following image.

enter image description here

The original image has resolution of 2000x1000, here it is

enter image description here

The image is automatically cropped from the right side, it should be fit with image view.


Solution

  • Pretty sure you didn't add constraints to the ImageView, the ImageView size it's not the same as the device size.

    Add constraints from the ImageView to the borders in the Interface Builder, and it will work.

    Check: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraints/WorkingwithConstraints.html