UIImageView display distorted image - ios

I use "ImageMagick" to distort my image on different points. In my example, I only changed the bottom right point. The image distortion works fine but I don't know, how to display the distorted image correct.
As you can see here, the image has a little distortion in the bottom right corner. But you can also see, that the image isn't displayed correct. You can't see the bottom right corner, because the image view cuts the image. Like "overflow: hidden" from css. How can I solve this problem?

try changing the contentMode as AspectFit or AspectFill as you need it
imageView.contentMode = UIViewContentModeScaleAspectFit

Try this:
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;

Related

how to set image to imageview center with background color on swift

I have an image as 30x30. I want to put this image into center of another 500x500 image and i want to define a background for 500x500 image like the following example. Is there any way to achieve this? Thank you.
Set the image as following, just keep in mind that image's background color should be clear.
imageView.image = UIImage(named: "search_image")
imageView.contentMode = .center
you can do this with the help of storyboard as shown in below picture.
and output is :
Set width/height fix for the image. Insert into the background view with constraints centerX and centerY.

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!

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.

Cropping or centering image in Swift

I have an image in an UIImageView. There are grey portions above and below the image because the image size doesn't have the same size as UIImageView. How can I crop or zoom the image in such a way that it just fits to cover the grey area above and below, or left and right?
you should set imageview's contentmode to UIViewContentMode.ScaleAspectFill and you should set propert clipsToBounds of imageview to true.
by this you will not get that gray space and image will fit in entire imageview.!!
Set the contentMode of the UIImageView to UIViewContentMode.ScaleAspectFill. You can do this programmatically or via Interface Builder.
yourImageView.contentMode = .ScaleAspectFill
See Apple Documentation for more details.

Resources