iOS: use calyer renderInContext to capture imageview lose quality - ios

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!

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

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: 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

If I have images that always need to be cropped to a certain size before going into a UIImageView, is cropping the most performant?

This is heavily related to my question here: Is there a way to get a high-res YouTube video thumbnail without letter-boxing?
Say I always get images back from an API that need to be cropped. They always have black letter-boxing at the top and bottom.
Now I could programmatically crop every UIImage down to the size it should be, but actually altering the image seems like a very expensive operation, and a waste when I really just need to hide it.
How would I best go about putting it in a UIImageView and making sure that the black bars aren't visible? To make it easier, it's safe to assume that it will be cropped into an aspect ratio that is the same as what the image is without the black bars (so in the above example, 16:9).
Resample or Crop the image accordingly and then
YourImageview.contentMode = UIViewContentModeScaleAspectFit // or you can use UIViewContentModeScaleToFill;
then...
YourImageView.layer.maskstobounds = yes; // or YourImageView. clipToBounds = yes;

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