Swift: Edit image taken from camera to fill a UIImageView - ios

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

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.

iOS: use calyer renderInContext to capture imageview lose quality

I use calyer's renderInContext to create a capture to imageView, but I find it's losing quality. Where was I wrong?
code:
result:
edit:
the above image is displayed by a imageView with contentMode of aspectFill and clipToBounds.
the below image is process by the code I give, actually, I just want to clip the image to the same size as the imageView with contentMode of aspectFill, so I don't need clipToBounds to cause offscreen-rendering.
according to the comment of #deadbeef, The quality of the bottom image is actually better (downscale is much smoother), which I don't know why, and as he suggest, try lowering the interpolation quality of your context if you want to achieve the same "sharp" result of the top image.
context.interpolationQuality = .low
I changed it to low, and it worked!

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.

Swift: is possible to get the default size of UIImageView used by UIImagePickerController?

In my iOS app I am developing, it is necessary for the user to take a photo. As you can see from the following image
by default, when we take a photo with iOS camera app, then it is showed with a standard/default size (the one delimited by those 8 pins) and I would like my app to be the same. So, which size should I set my UIImageView into which the photo will be displayed? How can I get that default size in Swift?
Or maybe...which would be the best size to give to UIImageview to prevent the photo from being deformed too much?
Thank you very much for you attention
UIImage has a property called size which specifies width and height of the image. So you could size your UIImageView to those.
Alternatively, if you're using constraints or autoresize mask (flexible width, flexible height) simply don't set a size and the UIImageView will fill itself according to contentMode.
You must understand however that what you see in the image you posted is not the "original size" of the image. Someone decided that the UIImageView should be place at X distance from top and bottom margins, thus forcing an implicit size on the UIImageView

Resources