How to constrain a UIImageView to the best size? - ios

In my IOS app I want to display the biggest variation of an image that can be fully displayed on the device. I want to do it without resizing the image to be larger or smaller so that the image doesn't seem blurry.
On my webserver I have 3 variations of the same image. To keep it simple let's say they only differ in width: 640, 750 and 1000.
An iPhone 5 will display the 640 width image.
An iPhone 6 will display the 750 width image.
An iPhone 6+ will display the 1000 width image.
I can easily get the size of a UIImageView in order to download the right image, but how to do I set an Auto Layout Constraint on the UIImageView that sets the UIImageView to the right width?
If I set a low priority <= 1000 width constraint on the UIImageView, then the image is displayed at a width of 1000 regardless of the size of the device. This is true even when I set the UIImageView to have a low Content Compression Resistance Priority.
I am using XCode, Objective C and IOS 8.
If I'm going about this wrong please tell me.
Thank you!

easy solution would be to make a width constraint of the UIImageView, then create an outlet for the same.
Now inside the code , set constraint.constant value by calculating according to the device.

Related

UIStackView: Aspect fill image squashed issue

I have 4 images inside a UIStackView which is constrained 20pts from top, right, bottom, left.
4 images are set to Aspect Fit and the UIStackView settings are set to: Fill Equally, and Spacing of 20.
When I come to view this on an iPhone 8 upwards the layout works. When viewing on an iPhone SE layout I get squashing of the images as below.
Shouldn't the fact that Aspect Fit has been set solve this issue and scale the image down accordingly?
Thanks
Add an aspect ratio constraint to each image and set it to 1:1. This should make the images have the same height and width, no matter the width of the screen.
I agree however, that using "aspect fit" should work. Are these regular images stored in your app bundle, or are you creating them with code or something similar?

UIImageView Content Mode for responsive image view frame

I have an image thats a particular height and width and its designed in such a way where its content is meant to be displayed at a minimum height, and as the height increases more of the image is allowed to be seen (meant to clip at the frame). This is because we wanted to serve up one image, but depending on device size, the frame can get display as much of the photo necessary.
My question is whats the best way to manage the content mode of the UIImageView to allow for this as well as how should the imageview constraints should be managed. Any advice? My original thought was to have the content mode be 'center' and use a combination of greater and less than height constraints to achieve the height part.
The red rect shows the uiimageview frame at its small screen size (iPhone SE), green is at normal size (iPhone) and orange is for plus devices.
Perhaps the community has a better solve for this?

iOS Button Vector Image doesn't automatically adjust to screen size

I am currently trying to introduce myself to Xcode, IB and Vector images. I am struggling with the auto layout and having the buttons adjust to screen size.
As you can see the buttons are same size in the iPhone 6s and iPad Pro.
And here is the iPad Pro 12.9" size of the buttons:
The vector images I use are universal .pdf images at the size of 50x41. And here are the settings to those:
How do I get these images to adjust properly from screen size to screen size?
Thanks a lot for your help!
What constraints do you have to change the size of the imageView? I would expect to see a constraint between the top two images (and the bottom two) that forces a constant gap.
The constraints on each image's width should then have a lower priority so they get stretched on a bigger screen. Then you want to constrain the height of each image to its width so they stay 1:1 (I'm guessing), and therefore stretch vertically if they are stretched horizontally.
Set the UIImageView content mode to UIViewContentModeScaleAspctFit and set AutoLayout constraints on the container for aspect ratios rather than just spacing and fixed sizes.

Image 1x, 2x and 3x issues on different device in iOS

I have 1x, 2x and 3x images in image assets file like below mentioned image.
But, it were placed 1x image for all devices like below mentioned image.
Am using Xcode 7.3 and iOS 9.3. I created imageview using auto layout. And set Align centre x to superview and Align centre Y to superview.
Any advice / input would be appreciated! Thanks.
This is fine. 3x doesn't mean that your imageview will show three time bigger. It's uses three time bigger resolution. That means if your imageview's size is 50x50 then iphone 6 plus (5.5 inch) requires image with resolution 150x150. If you set 50x50 resolution image to this display then it will not show perfact. That's it. It doesn't mean that 3x image will display 3 time larger then original imageview.
Then also you need your imageview of your size of your image then set UIViewContentModeScaleAspectFill content mode to your imageview with same constraints specified in question!
As #Lion answered this question very well, using 3X does not means your image height and width will increase 3 times.
If you want to resize your image view for bigger device so all you need is to use appropriate autolayout constrains.
follow this if you want to change origin and size of image view with device size(aspect ratio)
And when your size of imageview resize, use the 2/3 times bigger image as imageview for 2X/3X resolution.
Autolayout: origin and size should change according to width and height factor
Check these devices
iPhone 4 and prior #1X
iPhone 4S,5,5S,5C,6,6S,7,7S #2x
iPhone 6+,6S+,7+,7S+ #3x
suppose after applying constraints for bigger screen your image view's size is (50,50) then use a image of size 150,150 for 3X resolution
Make sure your content mode is UIViewContentModeScaleAspectFill and there are no height/width constraints for your UIImageView

UITableView cell auto height adjustment with image scaling issue iOS 9

I have a UITableView, which has a custom cell that I want to display a single image. I want the cell to automatically adjust the height according to the image's size. The UIImageView is filled in the cell, with auto layout constraints set on the four edges.
The images may be on different sizes, some are wider than the screen width. In such case, due to the content mode I set on the UIImageView, the image will be scaled to aspect fit the entire UIImageView.
When I set everything up and run, I realized that the UITableView didn't set the cell height correctly. It seemed that the image was scaled to fit the UIImageView but UITableView still set the cell height to be equal to the original image's height. Besides this issue, I also don't know why the image was scaled given that there are only 640 pixels on the width of the image and the iphone 6 simulator should have 750 pixels on the width ?
As summary, I have two questions here:
If I give an image with 640 pixels wide to an UIImageView that fills the entire width of the screen, why the image is scaled?
If the image is indeed scaled, how to let the UITableView automatically calculate the cell height based on the scaled size rather than the original size.
If possible, please use Swift instead of Objective-C for showing the code. Also you can assume I am using iOS 8+. Thank you.

Resources