How to set image UniformToFill stretch mode - ios

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.

Related

scale type or contentMode of Image in UIButton is not working

I was trying to put an image in UIButton and the image would fill up the space keeping the aspect ratio intact. I was not able to apply the content mode. Please see the screenshot
I also tried from the code and it's not working as well
myButton.imageView?.contentMode = .scaleAspectFill
By changing the alignment, the image takes the whole space but it does not keep the aspect ratio and content mode does not work as well.
What's causing your problem is how small the image is. There are two things you can do. One option is to set the image you have now as the background image and then change the button's constraints to get the proportions right. You won't be able to have the button be the size you have it now, though, because the image will be all stretched out.
The other thing you could do is find a bigger or different image of that symbol (square.and.arrow), rather than using the one already loaded into Xcode.

Displaying image with different aspect ratio in ImageView

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

Images not appearing properly in iOS

I'm facing an issue in UIImageView that images are not appearing properly. I am using collectionView in which I have used a custom cell. I have set:
cell.imageView.contentMode = .scaleToFill
I want images to look like this in Android:
You need to set contentMode to .scaleAspectFill. This will scale the image so that the entire image view is filled while maintaining the aspect ratio of the image. Your current setting, .scaleToFill, fills the image view but does not preserve the aspect ratio, so the image looks distorted.
Note that .scaleAspectFill may result in some image cropping if the image does not match the aspect ratio of the image view. You can see that this is occurring in your Android screenshot; the hair of the second person is out of view on the right of the first image.
You should set the content mode to .scaleAspectFill instead. It will fill the image view with the image but keeping it's aspect ratio.
You may use .scaleAspectFit if you want avoid cropping of your images. But in this case the image view is not entirely covered with the image.
First set ImageView to AspectFill mode,
imageView.contentMode = .scaleAspectFill
than Try cropping out the rest by using.
imageView.clipsToBounds = true
this should do the trick..
Either use
imageView.contentMode = UIViewContentModeScaleAspectFill;
or
imageView.contentMode = UIViewContentModeScaleAspectFit;
The first one will fill the frame, possibly cutting off parts of your image. The second one will show the entire image, possibly leaving areas of the frame open.

How to add a border to an image rather than the image view?

I am building a "photo gallery" for my app. When a user taps on an image I want a blue border to appear (to show that it is selected). Since I am using the content mode .scaleAspectFit, the image almost never takes up the full image view resulting in this. I want the border to be just around the image itself like this (the red border). How would I accomplish this?
My code...
imageView.image = images[index]
imageView.contentMode = .scaleAspectFit
imageView.layer.borderColor = UIColor.blue.cgColor
imageView.layer.borderWidth = 5
imageView.clipsToBounds = true
Make the aspect ratio of the imageView equal to the aspect of the image [ by creating all images to be same aspect ratio ] and give the imageView screen width minus border width
You can't achieve that effect with features of a UIImageView. You will need to customize something and there are a lot of approaches that could achieve the effect you want.
One easy solution is to do that scaling yourself. Aspect fit is extremely easy to compute. Here is an example I found.
Once you have computed the scaled image size, set the frame of the UIViewView to that size. Now your image will take up 100% of the UIImageView and your border will align properly!

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

Resources