UICollectionView image getting stretched - ios

My UICollectionView is stretching the images inside the cells.How to fit the image inside the cell?

Use following for setting content mode of UIImage
imageView.contentMode = UIViewContentModeScaleAspectFit;

For center cropping of image
imageView.contentMode = UIViewContentModeCenter;
you can also try this solution to best fit your need
Image with aspect fit and center cropping

Related

how to remove white space in circle UIImageView?

I wanted to circle my UIImageView and added this code :
profileImage.layer.cornerRadius = profileImage.frame.size.height/2
profileImage.clipsToBounds = true
and it work perfectly, but when images are horizontal, I get this picture:
as you can see, there is white space at the bottom and top of my circle image view. but what I really wanted was a circle filled with my image!
I've tried changing "content Mode" from attribute inspector, but I didn't get any answer! how can I fix this issue?
You have already set the clipsToBound property. So just update the contentMode.
I you don't want to distort image's scale:
profileImage.contentMode = .scaleAspectFill
If image's scale does not matter, you also can use:
profileImage.contentMode = .scaleToFill
Follow the link for more details: https://useyourloaf.com/blog/stretching-redrawing-and-positioning-with-contentmode/
Use below code:-
profileImage.clipsToBounds = true
profileImage.contentMode = .scaleAspectFill
Set the contentMode of your UIImageView to scaleAspectFill
profileImage.contentMode = .scaleAspectFill
Keep in mind that using this contentMode option some portion of the content may be clipped to fill the view’s bounds.
You can set the Different Content Mode as per you choice from StoryBoard:
You can select
Scale to Fill
Aspect Fill
or any other content mode by using trial and Error method which suits you
Hope it Helps.
If I choose Aspect Fit, I'm getting blank space in top and bottom of the imageview.
Can I trim that space anyhow?
Is there any programatically constraints I can set to re-scale imageview according to actual image size?

When I click the cell, the image is stretch out

This is the cell I created by XIB:
But however, after I use tableView load the cells, I get the issue:
You need to change image scale as below :
imageView.contentMode = UIViewContentModeScaleAspectFit;
Swift 3
imageView.contentMode = .scaleAspectFit
It is because you used Aspect Fill in the attribute pannel. Thus the image width equal to the screen (as per your constraints) and keep its width*height ratio, but it is going outside bounds of your imageView.
You need to add in viewDidLoad or in your class file:
imageView.clipsToBounds = true
Check this link to Apple's documentations
you need to clip the image out side bounds:
imageView.clipsToBounds = YES
it will crop image to visible rect only
If you design your cell in xib, you can set the Clip to Bounds in Drawing:

How to display UIImage without having any stretching but all be the same height?

I am displaying images in a UITableViewController inside a cell, currently to show the full image I am setting the UIImageView as aspectFit but it doesn't fill up the entire UIImageView.
Is there a way, where I can show the full image and not distort it like the other contentView modes do?
Hope this will help :)
YourImageview.contentMode = UIViewContentModeScaleAspectFill;
YourImageview.clipsToBounds = true
You should use as below :
Youreimageview.contentMode = UIViewContentModeScaleAspectFit;
This is from the This apple Document:
UIViewContentModeScaleAspectFit
The option to scale the content to fit the size of the view by
maintaining the aspect ratio. Any remaining area of the view’s bounds
is transparent.
For the Image showing in the Tableviewcell. You need to do customization of the UItableviewCell, and then you need to put imageview in that. That imageview must have this content mode to set image.

UIImageView doesn't resize itself to the UIImage it holds

I have a UIImageView with Auto-layout to the container margins. I set a UIImage to it in the ViewController. If I use UIViewContentModeScaleAspectFit then the image is centred in the middle of the screen as I wanted and everything looks great, but when I give the UIImageView a background color, I can see it still spreads all the way to the container margins, and doesn't get the image's proportions and dimensions. This is my code:
UIImage *passedImage = [UIImage imageNamed:self.photoTitle];
CGRect imageBounds = CGRectMake(0, 0, passedImage.size.width, passedImage.size.height);
[self.imageView setImage:passedImage];
self.imageView.bounds = imageBounds;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
I have tried everything to fix it and looked everywhere for answers, please help me, you are my only hope.
Instead of pinning the image view's edges to the container margins, pin its center X and center Y to the container's center X and center Y.
This gives the same visual result — the image is centered — but leaves the image view free to resize itself according to its contents.
UIImageView does not resize itself according to image size it renders. If you need to size it accordingly, you need to do it yourself. Matt's answer is reasonable, but you still need to update your image view size at some point. I'd suggest doing it in layoutSubviews or updateConstraints method of your view or in view controller's viewDidLayoutSubviews.

Fit UIImageView to image content

Is it possible to fit perfectly an UIImageView to its content, so that:
the origins of the image and the UIImageView object coincide;
the frame of the image and the UIImageView object coincide?
I tried using the following code:
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.imageView setBounds:CGRectMake(self.imageView.bounds.origin.x,
self.imageView.bounds.origin.y,
self.imageView.bounds.origin.x + self.imageView.image.size.width,
self.imageView.bounds.origin.y + self.imageView.image.size.height)];
However, in this way the UIImageView object starts at (0,0), while the image is centered on the screen.
Thanks in advance.
I think what you're looking for is something along the lines of:
[self.imageView setFrame: AVMakeRectWithAspectRatioInsideRect(imageSize, self.imageView.frame)];
Where imageSize is the aspect ratio you wish to maintain, and the in this case self.imageView.frame is the bounding rect.
This is part of the AVFoundation Framework so make sure to include:
#import <AVFoundation/AVFoundation.h>
From your description, it seem you want UIViewContentModeScaleAspectFill.
UIViewContentModeScaleAspectFit - will fit the image inside the image view. If the image is smaller than the image view it will be centered.
UIViewContentModeScaleAspectFill - will fill the image inside the image view. If the image is smaller/bigger than the image view it will be scaled.
Apple Documentation for UIViewContentMode
"Is it possible to fit perfectly an UIImageView to its content?" Yes. Your code attempts to set the bounds of the UIImageView. What it sounds like you want to do, is set the frame of the UIImageView to the size of the image.
Do a bit of Googling if you are unfamiliar with the difference between the frame of a UIView and the bounds of a UIView; it is an important distinction. If you want to set the actual size or location of the UIImageView, use its frame, which operates in the coordinate space of the UIView that contains your UIImageView. If instead you wanted to affect the coordinate space of views contained by your UIImageView, you would use the bounds.

Resources