问题描述
在iOS中,我的UIImageView是Aspect Fit,但是当我做CIFilter类型时,例如CISepiaTone,
我丢失了原始图像宽高比。图像被拉伸到整个UIImageView大小。
In iOS, my UIImageView is "Aspect Fit", but when I do CIFilter of type e.g. "CISepiaTone",I lose the original image aspect ratio. The image is stretched to whole UIImageView size.
如何保持宽高比?
推荐答案
如果使用imageWithCIImage创建过滤后的图像,则无法获得正确的宽高比。尝试从过滤器输出CIImage创建CGImageRef,然后从CGImageRef创建UIImage。
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.
示例:
// 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);
然后你可以尝试用filteredImage设置imageView。
Then you can try setting the imageView with filteredImage.
这篇关于如何在执行CIFilter时保留iOS UIImageView中的图像宽高比,如CISepiaTone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!