how to resize image without affecting its resolution in ios - 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

Related

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 :)

Can't get UIImageView to resize to fit screen

I've got an image which I want to display in a UIImageView and want it to automatically resize to fit the view, and fit the screen size of the device (iPhone 4, 6, and 6+ sizes).
However I've set the view mode of the UIImageView and its parent UIView to be AspectFit but the image doesn't scale to fit the view. Why does the image not shrink to fit within the UIImageView even though the view mode is set to AspectFit?
(I"m using Xcode 6)
Both aspect fill and aspect fit are used to size the image view to the size of the photo inside of it.
Aspect fit sizes your image so that the whole thing fits within the bounds of your image view.
Aspect fill sizes your image so that it fills up the whole image view (which can result in clipping).
More info on those definitions here.
What you most likely want to do is manually size your image view to fit the screen. The easiest way to do this might be to use something like the following, that way it will size to whatever device you're using.
[self.myImageView setFrame:self.view.bounds];

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.

I have few images and I want to show it on iPad on the full screen?

I have few images and I want to show it on iPad on the full screen - with same images. and image sizes are not same. Is there way to Stretch images (UIImageView) programmatically
Thank you
To stretch an image within UIImageView you just need to set the appropriate contentMode.
You can set the UIImageView to be the size of the screen and set the content mode like follows
3 options
// assume iv is an UIImageView the size of screen
// First option stretches your image to the size of the screen
[iv setContentMode:UIViewContentModeScaleToFill];
// Second option scales your image without changing aspect ratio to the largest possible size such that the whole image fits
[iv setContentMode:UIViewContentModeScaleAspectFit];
// Third option scales your image without changing aspect ratio to the smallest possible size such that the screen is covered (potentially clipping some of your image)
[iv setContentMode:UIViewContentModeScaleAspectFill];

Resources