Displaying image with different aspect ratio in ImageView - ios

I have a UIImageView of fixed Size 210*230 for movie Poster. the images coming from server for different movie are of different aspect ratio and size. How make all image good in that poster imageView. I need to keep the poster imageView of fixed size.

Use the contentMode property on the UIImageView. If you don't want any of the images to be cut off, you probably want to use scaleAspectFit so you would do something like myImageView.contentMode = .scaleAspectFit

You have two options here
Change the contentMode property of UIImageView to be scaleAspectFit. In this case, empty space would be shown inside image view if image view and image's aspect ratio is not same. If ratio is same, your image will fit perfectly inside imageView.
Change the aspect ratio of each image programatically to fit perfectly into the image view. Just google how to resize image in swift. I had done this my self, code is available on SO. Just search a bit.
Hope it helps

Related

How to set image UniformToFill stretch mode

I'm coming from XAML world where we have image stretch mode of Fill, UniformToFill, and Uniform. I'm targeting to achieve UniformToFill mode in my iOS app. In iOS, I came across ScaleToFill, AspectToFit, AspectToFill, and many others. None of them fulfill my requirement.
Required behavior: UIImageView (image container) size in layout remains fixed. Scale image according to fixed size of UIImageView and maintain aspect ratio of image itself. Upon matching either of side (vertical, horizontal), clip rest of the image.
To illustrate it better, here's the screenshot; I'm looking to achieve third from left.
Image stretch mode
XAML to iOS conversion is as follows.
Fill = ScaleToFill
Uniform = AspectToFit &
UniformToFill = AspectToFill
This is the exact match. If you are not getting this result, then it means you have done mistake in something else. Otherwise, this is perfect matching.
That's what .ScaleAspectFill does.
You are talking about a UIImage as a container ? I think you should look into UIImageView. Something like that would work ( that's written in Swift but easily adaptable to Objective-C
let image = UIImage(named: "your_image")
let imageView = UIImageView(image: image)
imageView.contentMode = .ScaleAspectFill
I think you should set UIViewContentModeScaleAspectFill as content mode of your image view.
You can refer Apple documentation for more details.
It states about scaleAspectFill,
The option to scale the content to fill the size of the view. Some
portion of the content may be clipped to fill the view’s bounds.
scaleAspectFit
The option to scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
Try both to feel real difference.

How to configure the image or image view so that the image keeps its aspect ratio no matter what the size of the image view is?

I put an image view in a static table view cell. The image is now being stretched to fit the whole view. I want the image to:
be centered in the image view
be in the largest size possible but
not exceed the bounds of the image view and
keeps the aspect ratio of the image
First I went to the docs of UIImageView. But didn't find anything useful. Then I looked at the docs of UIImage and found UIImageResizingMode. But that enum only contains Stretch and Tile, which is not what I wanted. I then found this post which told me to
logo.contentMode = .Center
But the image is still stretched. I then tried this
logo.contentMode = .ScaleAspectFill
But the result is the same.
Please tell me a good way to do this!
Additional info:
The image view has the following constraints:
You have to set the contentMode to UIViewContentModeScaleAspectFit.

Swift: Edit image taken from camera to fill a UIImageView

I have used a UIImageView that takes its image from the default camera app using UIPicker. But the image I am getting is too small to fit into the image view and if I use:
imageView.contentMode = UIViewContentMode.ScaleToFill
While this indeed scales to fit the image view the picture looks like it has been mashed up and not particularly good. I was wondering if it was possible to scale the image up in such a way that the quality of the image does not get bad.
I am using Swift 1.2 and thanks for any help! :D
It will work with:
myImageView.contentMode = .ScaleAspectFit
Camera images are usually bigger than a UIImageView on a device. To shrink them quickly to a visible size, the best way is to set the .contentView property of UIImageView to .ScaleAspectFit.
try using UIViewContentMode.ScaleAspectFill
that fills the imageview by keeping the original aspect ratio. but that also means that parts of the image could be outside of the bounds of the imageview and therefore not visible.
If you use a uiImagePickerController, you can adjust the quality with the property videoQuality : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImagePickerController_Class/#//apple_ref/occ/instp/UIImagePickerController/videoQuality
Juste set the quality type as TypeHigh

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.

UIViewContentModeScaleAspectFill not maintaining aspect ratio of image

I am using UIImageView in UICollectionViewCell. I have set contentMode to UIViewContentModeScaleAspectFill and Clip subviews to YES.
Still image's aspect ratio is not maintained. It shrinks image width/height.
Please note that I know I can center crop image through code but I am looking for a way to do it without using code to change image size.
Also I can't use UIViewContentModeCenter because real image is much larger than UIImageView size.
For maintaining Aspect Ratio you have to use UIViewContentModeScaleAspectFit instead of UIViewContentModeScaleAspectFill.

Resources