Filling UIImage view with a small image - ios

I'm working with a UIImage, when the image is larger than the image view, I'd like to aspect fill.
When the image is smaller, I'd like the same for it to stretch it, keeping the aspect ratio but filling it's UIImageView.
Currently, it will just leave the smaller image in it's original size..

Related

UIImageView contentMode Scale Aspect Fill is Stretching the Image (Nevermind, It Wasn't)

I feel like I'm going crazy. I set my UIImageView.contentMode = UIViewContentModeScaleAspectFill, but it is still stretching the image. (Not a clipping issue, it is stretching the image.)
It even shows it stretched in my Storyboard. I created an image of green and brown squares to test:
I added a UIImageView in my Storyboard. I added the image to that image view, and when I'm setting to Aspect FIT then it does what it is supposed to do (maintain aspect ratio and show the whole image allowing gaps as needed), but when I switch to Aspect FILL then it stretches the image. Here is an animated gif from my storyboard:
What the heck is going on? This is in a storyboard in a project that is a few years old. If I create a brand new project with a brand new storyboard, then this insanity doesn't happen.
Is there something in my underlying storyboard that is causing this bug?
Any help is appreciated. I feel like I'm taking crazy pills.
It's not stretching it. It's filling the area while keeping the aspect ratio. If it were stretching it, the horizontal white line in the middle would not be as tall as the vertical white lines are wide.
Set the content mode to "Scale To Fill" to see what it would look like if it were getting stretched (like this):
Note the difference in line thickness of the middle white line, as opposed to when it's set to Aspect Fill:
If you turn off Clip to Bounds on the image view, then you can see how the image is getting cropped when set to Aspect Fill:
If you want to show the full image at the full width while keeping the aspect ratio, you'll have to increase that cell's height to accommodate the height of the image.

Trying to clip edges from UIImage to fit to ImageView

I have added an icon to my assets to be used in an image view. However, When I put it in an image view the outer edges seem to be larger than the image itself. Is there any way I can fully fill the image. Even when I try scale to fill or aspect fill, fit options still there remains a margin in between.
In other words I want my circular image to be tangent to the image view rectangle
The example of what I am trying to do

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

UIImageView Aspect Ratio?

I have an image of size 320X460 and I want to create an UIImageView which height should be 450. To maintain aspect ratio I calculated the width of UIImageView = (320/460)*450 = 313.043 dynamically. And set the contentMode For UIImageView is UIViewContentModeScaleAspectFit. And set the image(320x460) to image view but it is some what blur.
Note: If I don't resize the UIImageView to 313.043X450 the image is very clear as it is. So what is the mistake I have done?
If I understand the question, this should answer it.
First to set the aspect ratio for the image in your image view.
myView.contentMode = .scaleAspectFit
Next, your image may blur because it is a .png or another rasterized format. You need to use .pdf as recommended by Apple or at very least another vectorized format. Rasterized images have values for all pixels in the image, so when the image is stretched too far it just duplicates and blurs pixels. Vectorized images do not blur because they are really just a series of instructions on how to draw/render the corresponding image.
If you are resizing UIImageView manually, set the content mode to UIViewContentModeScaleAspectFill.
If you want to keep content mode UIViewContentModeScaleAspectFit do not resize imageview to 313. Image will adjust maximum possible width and height , keeping it's aspect ratio.

Zoom image until it fits ImageView Completely

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.

Resources