Fit UIImageView to image content - ios

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.

Related

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.

Resizing UIImageView without scaling UIImage

I have a UIImageView that display a UIImage with the content mode set with UIContentModeScaleAspectFit. The imageview is as width as the screen. I want to resize the imageview without changing image position and scale. So, after resizing, the UIImage would be still virtually at the same position and still virtually as width as the screen, but not entirely visible because of the imageview resizing.
I try using UIContentModeLeft, while resizing from the right, to fix UIImage position, but my image wasn't at full resolution like when I'm using UIContentModeScaleAspectFit.
I'm clueless about this. I might need to use CGImage or CIImage, but I don't know where to start.
Set the image you want to display as the background of the view, instead of the source image.
You can use:
yourImageView.background = [UIColor colorWithPatternImage:[UIImage imageNamed:#"nameOfTheImageYouWantToDisplay"]];
I manage to solve my problem. Here's what I did :
Create a UIView in which I add a UIImageView subview
Set the imageview content mode to UIContentModeScaleAspectFit
Defining imageview frame to the screen with [[UIScreen mainScreen] bounds]
setClipsToBounds = YES to the UIView
Now, when resizing, the imageview is not scaling because its frame is define to be as big as the screen, setClipsToBounds: make sure I don't see part of the image that is outside of the UIView frame.

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.

xcode UIView ScaleAspectFit for UITextfield setLeftView

I'd like to have an icon in my textfield and I'm using a the setLeftView attribute.
my code at the moment is
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 26, tb_teamA_playerA.bounds.size.height)];
leftView.contentMode = UIViewContentModeScaleAspectFit;
[leftView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed : #"person_26" ]]];
[tb_teamA_playerA setLeftView :leftView];
[tb_teamA_playerA setLeftViewMode: UITextFieldViewModeAlways];
that's the result.
the very left image is a separate UIImageView with the same image set and the scale set to ScaleAspectFit.
So I was expecting the two images to be the same size and scale but they are not?!
You are using UIViewContentModeScaleAspectFit, so the image will fit the size of the UITextField.
I think you should use UIViewContentModeScaleAspectFill, and if you have some problem with ths size and the two images still don't have the same size, you should resize you image.
The following is from the Doc
UIViewContentModeScaleToFill
Scales the content to fit the size of itself by changing the aspect ratio of the content if necessary.
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.
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.

UIImageView overflowing its containing UIView after calling sizeToFit

My UIView (width: 352px) has a UIImageView subview (default width in Storyboard: 312px).
I want the UIImageView to adapt itself to the dimensions of the image it contains with the constraint that the width of the image view shouldn't exceed a maximal width size (in my case, 312px).
I set up the 'autosizing' configuration of my UIImageView to have a fixed left, top and right margin size. Nevertheless, when I call sizeToFit on my UIImageView and its image is larger than 352px, the UIImageView gets wider than its containing UIView.
Is there a convenient method to prevent such a behavior without doing the math based on the image dimensions? Am I using sizeToFit the right way?
Just use setClipsToBounds:
[imageView setClipsToBounds:YES];

Resources