Is it possible to trim the bottom of the image that extend beyond the frame. I've got an image height of 280px, and the frame is targeted at 240px, if you compress the height, the image loses its quality.
I turned on the mode - Aspect Fit and enable "Clip Subviews" and also have auto-layout within the maximum image height 240px
In the pictures, the text must be below the pictures, but not inside it.
Related
As the title says, I'm looking for a way to preserve the ration of width:height when one of those two changes.
If you set the image of the UIImageView, the frame gets set accordingly. Say the image is 10x10, so the imageView has a 10x10 frame. If you now shrink the width of the imageView to be 5 wide, the image can appropriately scale to be 5x5, however, the imageView becomes a 5x10 imageView, adding transparent blocks above and below the image.
Currently I've set a height constraint that mimics the ratio of the image I want to set, however, if the image is ever updated or changed to one with a different ratio, it will mess up how the image is displayed.
Is there a generic way to tell the imageView to automatically scale it's width and height to be the same original ratio of the image, or is there any property on UIImageView to allow it to preserve it's width:height ratio when changing only one of the properties?
Use aspect ratio Autolayout constraint.
Set Mode to Aspect Fill.
Check Clip subviews.
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.
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
I have a simple UIImageView and an image. I want to fit the image in that UIImageView but I don't want the image aspect ratio to change and I also don't want any dead spaces. ( black bars on the sides etc') I don't care if the image is zoomed in all the way as long as those 2 rules are applied.
Is there a build in setting for that? I tried all the Scale To Fill and Aspect Fill etc' but couldn't find what I'm looking for.
For example: UIImageView is 300x300
image is 200x250. The image will zoom in until all the areas of the UIImageView are filled.
For a UIImageView you can use Aspect Fill in the properties to do this.
But you may have to tick the box "Clip Subviews" otherwise the image will spill outside the image view frame.