Cropping or centering image in Swift - ios

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.

Related

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!

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