Can't get UIImageView to resize to fit screen - ios

I've got an image which I want to display in a UIImageView and want it to automatically resize to fit the view, and fit the screen size of the device (iPhone 4, 6, and 6+ sizes).
However I've set the view mode of the UIImageView and its parent UIView to be AspectFit but the image doesn't scale to fit the view. Why does the image not shrink to fit within the UIImageView even though the view mode is set to AspectFit?
(I"m using Xcode 6)

Both aspect fill and aspect fit are used to size the image view to the size of the photo inside of it.
Aspect fit sizes your image so that the whole thing fits within the bounds of your image view.
Aspect fill sizes your image so that it fills up the whole image view (which can result in clipping).
More info on those definitions here.
What you most likely want to do is manually size your image view to fit the screen. The easiest way to do this might be to use something like the following, that way it will size to whatever device you're using.
[self.myImageView setFrame:self.view.bounds];

Related

Make UIImageView fit width of screen but keep aspect ratio [swift]

I'm trying to build a simple tableview with a tableView showing a list of images people posted and comments their friends added.
This is a subclass of TableViewCell, and I used stack views to lay it out. The problem is that I want to show the photos without stretching them - I want them to keep their original aspect ratio, but people can upload images of any dimensions. You see in the screenshot that the image is squished.
What I noticed is that the larger the uploaded .jpeg file, the taller the cells grew (I'm guessing the image size is dictating the intrinsic height?), but since the width can't change, each image is squished differently. A huge picture will show up super tall but with the same width.
I tried setting the image to Aspect Fit, but only the visible image itself does what I want: fit to width and not warp the aspect ratio. However, the UIImageView itself is still super tall and pushing all other UI elements out of the way.
I must be missing something here... It shouldn't be so hard, should it?
To constraint the image as aspect ratio , first give it width of screen , then give it a static height in IB and hook that constraint as IBOutlet
and set it inside cellForRow
cell.imageHeightcon.constant = imageRealHeight*imageWidthInIB/imageRealWidth
You should fix the size of the image view for whatever you want. Then you should use aspect to fill instead of aspect to fit and select the option clip to bounds in the attribute inspection

How to correctly use aspect ratio in xcode

So I'm trying to create a user profile that has a user picture that changes sizes with the screen size so it doesn't take up too much space on the smaller iPhone screens.
Everything else in my simulator is sizing correctly during simulation, but the user picture changing size correctly without breaking the ability to stay circular. I tried using aspect ratio, but the picture stays the same size when I simulate on smaller screens. I tried using aspect ratio in relation to the whole view (so that it changes sizes depending on the view), but since the view is rectangular, the picture cannot hold it's ability to stay circular (since it needs to be a square to correctly make the picture circular).
How would I achieve this without creating different views for each phone screen size?
If you set the contentMode of your UIImageView to UIViewContentModeScaleAspectFit, it doesn't matter if the view itself is not square - the image will always show in the right aspect ratio.

How to figure out dimensions of image to use from Storyboard?

I have a UIImageView in a screen in my Storyboard. From the Dimensions of the image in the Storyboard, what size image should I exporting from my photo editor (like Illustrator) so that the image does not look bad / stretch / out of proportion?
iOS Noob here -- trying to figure out what dimensions of the image to request from my designer.
If the imageView is constrained so its size never changes, you can just find the size of the view in the size inspector and use that. If the imageView or image shape might change, you can set the imageView to Aspect Fit or apply constraints that allow different aspect ratios.

how to resize image without affecting its resolution in ios

I have an image view in which I have set image.It works fine if the size of image and image view is almost same. But what if we change the device, Image view is inc in size but the image gets stretched. I am unable to find any way to resize this image such that the image is not affected by the size of image view and at the same time remain centralized also. Also resolution of the image should not be affected.
you can use
[imageView setContentMode:UIViewContentModeScaleAspectFill];
or
[imageView setContentMode:UIViewContentModeScaleAspectFit];
Aspect Fit: Will size your image until the whole image will fit within your UIImageView Box. Thats why you are left with the extra space on top and bottom.
Aspect Fill: Will size your image proportionally until the whole UIImageView is full of your image. So that is why you see clipping of your image. It will actually size it proportionally to make sure there is no blank space left in your imageview.
In a nutshell: Aspect Fit makes sure your whole image is visible proportionally. Aspect Fill will make sure there is no space left in the imageview while sizing your image proportionally.
so it is upon you what you can compromise.
a rendom google image for example.
use this imageViewName.contentMode = UIViewContentModeScaleAspectFit;
as per docs This option is used to scale the content to fit the size of the view by maintaining the aspect ratio

iOS Aspect Fill Image Resizing with Content Aligned to Left/Top Edge

I am developing a universal app for iOS-7. this particular question is specific to iPad only. I want to place an image as background of root view of a View Controller i.e. the image should fill the whole screen. The 1x image has size:768x1024. This works for Portrait orientations as non-retina resolution of iPad is also 768x1024 in portrait. In Landscape however the image does not fit. I have tried using ScaleToFit but since aspect ratio of image is not preserved I can not use ScaleToFit(their are things in image which look odd when not scaled proportionally in both axis). AspectFill resizing seems most suitable for my need, but their is a small problem. As defined in Apple Documentation "The content is resized to completely fill the bounds rectangle, while still preserving the aspect of the content. The content is centered in the axis it exceeds.". I do not want the content to be centered in axis in which it exceeds, I want it to be aligned to top/left edge.
So basically I want two things:
Aspect Fill
The content remains aligned on the Left/Top edge.
Is it possible to achieve this. Any code-snippet will be great.
Thanks
You will have to subclass the View Controller's view and manually scale and align.
Look at the code snippets in THIS answer.
Adjust imageViewXOrigin and imageViewYOrigin to align however you want.
Really the only way to get around the content fill mode is to have two different images, one for each orientation. I'd suggest changing the image in the view controllers willAnimateToOrientation: method so that you can put the image changes inside UIKit's animation block.

Resources