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.
Related
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.
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.
I have a UIScrollView with a single UIImageView child. The scrollView is pinned on all sides to the (root) parent container with autolayout, and the child imageview is also pinned to all sides with content mode set to AspectFill.
UIImage *image = [UIImage imageNamed:#"photo1.jpeg"];
_imageView.image = image;
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_scrollView.contentOffset = CGPointZero;
_scrollView.contentSize = _imageView.image.size;
_scrollView.zoomScale = 1;
The image in this case is wider than my screen. When I launch my app, the imageview is correctly displaying the image filling up the screen. However, I can't seem to pan the image left or right. When i zoom in, I can pan, however, I'm unable to pan to the corners of the image.
I feel like my content size is not being computed properly thanks to auto-layout, but I'm not sure which parameters I can play which would allow me to scroll the image to its edges.
I've attached an image below, where you can see that the image can't be scrolled in a way that displays the beginning of 'happiness'. I also uploaded the sample here
the child imageview is also pinned to all sides
Instead, turn off auto layout for the child image view (set its translates... to YES). You are already setting the scroll view's contentSize to the image size, so now scrollability will leap into life and will interface correctly with zooming.
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.
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];