简体   繁体   中英

How to preserve image aspect ratio in iOS UIImageView when doing CIFilter, like CISepiaTone

In iOS, my UIImageView is "Aspect Fit", but when I do CIFilter of type eg "CISepiaTone", I lose the original image aspect ratio. The image is stretched to whole UIImageView size.

How can I preserve the aspect ratio?

If you are creating the filtered image with imageWithCIImage, then you won't get the proper aspect ratio. Try creating a CGImageRef from the filter output CIImage and then creating the UIImage from the 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 setting the imageView with filteredImage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM