UIImageView image distorts when big image is place

2019-06-27 09:45发布

问题:

While trying to set a big image in UIImageView the resolution of the image distorts. Is there any way to keep the image in same resolution while fitting it in a smaller view?

回答1:

try setting imageview content mode to aspectfit.

imageView.contentMode = UIViewContentModeScaleAspectFit;


回答2:

UIViewContentModeScaleToFill
Scales the content to fit the size of itself by changing the aspect ratio of the content if necessary.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeScaleAspectFit
Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeScaleAspectFill
Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeRedraw
Redisplays the view when the bounds change by invoking the setNeedsDisplay method.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeCenter
Centers the content in the view’s bounds, keeping the proportions the same.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeTop
Centers the content aligned at the top in the view’s bounds.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeBottom
Centers the content aligned at the bottom in the view’s bounds.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeLeft
Aligns the content on the left of the view.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeRight
Aligns the content on the right of the view.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeTopLeft
Aligns the content in the top-left corner of the view.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeTopRight
Aligns the content in the top-right corner of the view.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeBottomLeft
Aligns the content in the bottom-left corner of the view.
Available in iOS 2.0 and later.
Declared in UIView.h.

UIViewContentModeBottomRight
Aligns the content in the bottom-right corner of the view.
Available in iOS 2.0 and later.
Declared in UIView.h.


回答3:

Setting the size must occur AFTER setting the contentMode

[imageView setContentMode: UIViewContentModeScaleAspectFit];
imageView.frame = CGRectMake(x, y, width, height);


回答4:

I fixed this problem by resizing the image to size of the UIImageView that holds the image.

For me neither, imageView.contentMode = UIViewContentModeScaleAspectFit nor setting the frame of the imageView worked.

Scenario:

I am using a UITableViewCell from Xib and it has an UIImageView. I set the image for the UIImageView programatically.