UIImageView with full width and variable height - ios

I am developing an app where I want the UIImageview like instagram feed. I set the leading and trailing space to mainview as zero so width is now 100% of screen. Now how do i set the height so that it varies depending on image's scale. I tried setting content mode to Aspect Fit, but its not working and blank spaces are coming in the left and right side for a portrait mode image
Sample Image i selected
How it came after selecting in UIIMageView

To solve this issue you need to get Image dimension in Height and Width from server side for each image.
Then Accroding to Image Width, Image Height, Screen width you have to calculate new Image Height and Set it to ImageView Height by taking Image View Height Constraint.
let calculatedImageHeight = (screenWidth * imageHeight)/imageWidth

Related

How to make a UIImageView take up a fixed percent of the screen regardless of device?

Assume I have one big UIImageView that is set to a vector, and I want this ImageView to take up 90% of the screens width while maintaining it's aspect ratio, regardless of device, since it is a vector, resizing is not an issue. Height does not matter since I need it to just maintain it's aspect ratio, so long as it is 90% of the width of the screen. How can I do this in UIBuilder?
Here is how to make an image width 90% of its superview:
Add the UIImageView to the Storyboard.
control-drag from the imageView to the background view.
Select Equal Widths from the pop-up.
Change the multiplier for the constraint to 0.9.
To set up the aspect ratio, control-drag from the UIImageView to itself and select Aspect Ratio from the pop-up. Set the multiplier value of the resulting constraint to the desired value. You can use a ratio such as 3:4 or a floating point value such as 0.75.
In the Attributes Inspector set the Content Mode of the UIImageView to Scale To Fill, Aspect Fit, or Aspect Fill so that the UIImage fills the UIImageView.

Using "Aspect Fit" cuts off image

I have an image added to the storyboard with the following constraints:
align CenterY to superview
align CenterX to superview
image width = superview width * 0.43
mode: Aspect Fit
The goal of this is to adapt the image size depending on the screen width.
It does work on iPad, but on iPhone some of the letters of my image are cut off (in this case the top of the O). I tried to make the image larger by adding some space on top of the PNG (transparent pixel). But that didn't changed the behavior. I could use fixed sizes, but shouldn't my approach work too?
Try AspectFill mode instead of AspectFit and set more one constraint is fixed height, whatever height you want. If you want exact square imageview then height should be same as width(i.e. superview width * 0.43).
Now I switched to jpg. Don't know if the transparency or the format (png) was the issue, but for the launchboard iOS has some problems with scaling an image. Especially if there is some curves and transparency.

How to resize UIImage vector graphic

This is a rather strange question. You're probably thinking "You must resize the UIImageView, not the UIImage", but I can't get it working, and I'm not sure it's correct in this case.
I have a .pdf vector image I want to use in an iOS application. This vector image was exported to .pdf with the default size of 320 width. Of course, this is vector, so it should scale perfectly.
When I create an UIImageView using that image (UIImageView(image: vectorImage)) it always gets a width of 320 (or scaled #2x/#3x).
I have constrained the UIImageView to have margin 0 left and right. On iPhone 5, this displays correctly, since it's a 320-based size screen. However, on iPhone 6 - the resolution is different, with a 375-based screen width.
Even though the imageView is constrained to the full width (375), the UIImage within still has a size of 320.
I never set the height.
To clarify, I'm using ScaleAspectFit. This should not cause the image to be stuck at 320 though. As I never set the height of the UIImageView, it should begin with a correct fit.
I can probably solve this if I have the aspect ratio for the image, but that seems unnecessary!
A correct result would be for the image to fill the entire width of the imageView, and the height of the imageView to change to fit the image.
The actual result is that the height and width of the UIImage is the same for all devices (relatively), and the height for the UIImageView is the same for all devices. Due to the constraints added, the width of the UIImageView is different, but the UIImage within is not changed. Why not?
I suspect that if the vector-image had been exported with a different default size, the result would be different. Why is this?
This post gets a lot of views so maybe worth a quick answer in case it helps anyone.
When using vectors you can't set the image and to the UIImageView and expect it to work out its height from the width, even when setting it to ScaleAspectFit.
One way to resolve this is to get the UIImage, get it's size and then use this to set the UIImageView height constraint.
For example:
let image = UIImage(named: "name")!
let ratio = image.size.height / image.size.width
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: ratio).isActive = true

Why is this object not positioned properly with Auto Layout?

I place an image view (content size: aspect fit) on my base layout. I create the trailing, leading and bottom constraints and set their constants to 0.
This doesn't happen when the image is smaller than the view:
Though, the image isn't positioned properly on the iPhone - it appears almost centered - while it is on the iPad. Perhaps its intrinsic content size is too big? Why is this happening?
As you haven't set a top, aspect ratio, or height constraint on your image view, iOS falls back to the intrinsic content size (the actual size of the image) for the height only (the width is set by the leading/trailing constraints). If the image is wider than the screen, it will result in an image view that is as tall as the original image, but with the with set to the screen width.
Then, as you have the content mode set to Aspect Fit, iOS places your resized image inside the image view, and leaves lots of blank space around it.
One option to resolve this is to add an aspect ratio contraint on the image view, matching the aspect ratio of the image inside it. This will result in a correct height for the image view.
beyowulf was correct i believe. You should try changing the background color of imageview. If you change the imageview background color, you will realise that imageview always obeys your auto-layout constraint.
Now why not image??
Its because you asked it not to :) aspect fit will try to resize the image still maintaining the aspect ratio :) when the size of the image is too large i.e greater than the size of imageview frame, image covers the full frame of imageview and maintains the aspect ratio as well. Meaning if width is greater than screen width, image will cover the imageview frame width and takes a corresponding height for that width.
Thats why in first case your image covers full imageview frame where as in second where image size is small covers only space required :)
If on the other hand you want image to cover imageview frame always either fall back to scale to fill or aspect fill based on your requirement.
Happy coding :)

How do I get a square image to scale up evenly if the phone screen size increases?

I am having trouble figuring out how to set up the constraints on an image view.
I have an imageView of size 155*155. I want the view to remain a square shape but scale up if the iPhone screen size increases to 6 or 6+. I can constrain the distance to the bottom of the container and the sides of the imageView to the sides of the main view. This should stretch the width but is there a way to make the height increase by the same amount? Any pointers would be really appreciated. Thanks
You can use an aspect ratio constraint by ctrl + dragging from your image INTO your image.
This basically tells that you want to maintain the current aspect ratio as your other constraints force a change in the width or in the height.

Resources