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
Related
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
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.
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 :)
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.
Please see screen shot of flower setup below. The flower image has been correctly loaded from an asset catalog and when the app is run on various simulators the correct pixel resolution is assigned to each device. My problem is how to get the flower image to be scaled (equally sized to fit) the same on each device ??
I have learnt how to position the image to different positions using constraints and frames but the image never scales correctly - please see first pic
The following image is a mock up of what I want to be able to do (flower image scaled correctly on each device)
Judging by your mockups, it looks like you want the image to fill half the width, and keep its square aspect ratio to determine its height. One way to approach this would be to use AutoLayout to make a left UIImageView and a right placeholder (blank) view. Pin the left view to the left edge of the parent, the right view to the right edge, and then set them to be 0 pixels from each other. Then set an equal widths constraint on them. Finally, control drag the image view to itself and you can select aspect ratio -- and assuming that in IB, the width and height is the same, it will keep it square. Adding an equal heights constraint will give the other view the height it needs to be equal in case you need that.
This gives you a left image view that is 50% and with your mode set to Aspect Fit or Aspect Fill, it should give you the results in your mockups. In case you have an image that isn't square, make sure to check Clip Subviews for your UIImageView to prevent showing the overflow.
The real problem is that your goals are not well defined. Scaled with respect to what? The screen has a height and a width. You can't scale with respect to both, because that would distort the image (because different devices have different overall aspect ratios). Thus you have to pick one dimension that you will scale to.
Judging from your mockup, I'm going to guess that what you want is to scale with respect to height. If so, then give the image view a height constraint that is a fixed fraction of its superview's height. It looks to be about 0.25 in your mockups but you will have to eyeball what you think is a good fraction.
Now, if the content mode of the image view is Aspect Fit (or Aspect Fill), the image will change its height along with the image view without distorting the aspect ratio.
However, it would be best if you would make the other dimension (here, the width) of the image view a fixed fraction of the height, such as to make the aspect ratio of the image view the same as the aspect ratio of the image. The reason is that otherwise the image might end up centered in the image view in such a way that it doesn't touch the top or left any more, even though the image view itself does.
CGFloat screen_width = [[UIScreen mainScreen] bounds].size.width;
your_imageview.frame = CGRectMake(0, 0, screen_width/2, screen_width/2);