UIImageView aspect fit and center
Solution 1
Just pasting the solution:
Just like @manohar said
imageView.contentMode = UIViewContentModeCenter;
if (imageView.bounds.size.width > ((UIImage*)imagesArray[i]).size.width && imageView.bounds.size.height > ((UIImage*)imagesArray[i]).size.height) {
imageView.contentMode = UIViewContentModeScaleAspectFit;
}
solved my problem
Solution 2
In swift language we can set content mode of UIImage view like following as:
let newImgThumb = UIImageView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
newImgThumb.contentMode = .scaleAspectFit
Solution 3
Swift
yourImageView.contentMode = .center
You can use the following options to position your image:
scaleToFill
scaleAspectFit
// contents scaled to fit with fixed aspect. remainder is transparentredraw
// redraw on bounds change (calls -setNeedsDisplay)center
// contents remain same size. positioned adjusted.top
bottom
left
right
topLeft
topRight
bottomLeft
bottomRight
Solution 4
Updated answer
When I originally answered this question in 2014, there was no requirement to not scale the image up in the case of a small image. (The question was edited in 2015.) If you have such a requirement, you will indeed need to compare the image's size to that of the imageView and use either UIViewContentModeCenter
(in the case of an image smaller than the imageView) or UIViewContentModeScaleAspectFit
in all other cases.
Original answer
Setting the imageView's contentMode to UIViewContentModeScaleAspectFit
was enough for me. It seems to center the images as well. I'm not sure why others are using logic based on the image. See also this question: iOS aspect fit and center
Solution 5
I solved this problem like this.
- setImage to
UIImageView
(withUIViewContentModeScaleAspectFit
) - get imageSize
(CGSize imageSize = imageView.image.size)
-
UIImageView
resize.[imageView sizeThatFits:imageSize]
- move position where you want.
I wanted to put UIView on the top center of UICollectionViewCell
.
so, I used this function.
- (void)setImageToCenter:(UIImageView *)imageView
{
CGSize imageSize = imageView.image.size;
[imageView sizeThatFits:imageSize];
CGPoint imageViewCenter = imageView.center;
imageViewCenter.x = CGRectGetMidX(self.contentView.frame);
[imageView setCenter:imageViewCenter];
}
It works for me.
Comments
-
Ravi Vooda almost 2 years
I have an image view, declared programmatically, and I am setting its image, also programmatically.
However, I find myself unable to set the image to both fit the aspect and align centre to the image view.
In other words, I want the image to:
- Scale down to fit the aspect, if the image is large.
- Centre but not scale up, if the image is small.
How do I get that?
-
Manu about 11 years[yourImageView setContentMode:UIViewContentModeCenter]; and make sure if imageView Frame is greater than image frame. if it's not then place [yourImageView setContentMode:UIViewContentModeAspectToFit];
-
Manu about 11 yearsif else condition dude @RyanPoolos
-
Skrew over 10 yearsThis will ignore the scaling
-
Cfr over 9 yearsShorter way to find out image is smaller:
CGRectContainsRect(imageView.bounds, {CGPointZero, image.size})
-
Timur Bernikovich over 7 years@Cfr maybe it's shorter, but not so readable. At the same time I propose to split line with if-statement like this:
CGSize imageSize = ((UIImage*)imagesArray[i]).size;
,CGSize viewSize = imageView.bounds.size
,if (viewSize.width > imageSize.width && viewSize.height > imageSize.height) {
. -
NoelHunter about 7 yearsShould be UIViewContentModeScaleAspectFit
-
Arjay Waran almost 7 yearsimageView.contentMode = UIViewContentModeScaleAspectFit;
-
slow about 6 yearsI don't know why solutions like this got so many upvotes. This person clearly didn't read the question at all.
-
fishinear over 5 yearsThis blows up the image if it is smaller than the image view, contrary to what the OP asked for.
-
fishinear over 5 yearsThis blows up the image if it is smaller than the image view, contrary to what the OP asked for.
-
fishinear over 5 yearsThis blows up the image if it is smaller than the image view, contrary to what the OP asked for.
-
Nate Cook over 5 years@fishinear The requirement about not scaling the image up was added to the question a year after I answered it: stackoverflow.com/posts/15499376/revisions