How to save image aspect ratio in iOS UIImageView when executing CIFilter like CISepiaTone

In iOS, my UIImageView is an β€œAspect Fit,” but when I do a CIFilter type, for example. "CISepiaTone", I am losing the original aspect ratio of the image. The image is stretched to the full size of the UIImageView.

How to keep aspect ratio?

+4
source share
1 answer

If you create a filtered image using imageWithCIImage, then you will not get the proper aspect ratio. Try creating a CGImageRef from the output file of the CIImage filter, and then create a UIImage from CGImageRef.

Example:

// assume you already have CIImage *outputImage and *inputImage declared // convert to CGImage to maintain proper aspect ratio and dimensions CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef imageRef = [context createCGImage:outputImage fromRect:inputImage.extent]; UIImage *filteredImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); 

Then you can try to set the image using filterImage.

+4
source

Source: https://habr.com/ru/post/1497118/


All Articles