【问题标题】:How to preserve image aspect ratio in iOS UIImageView when doing CIFilter, like CISepiaTone做CIFilter时如何在iOS UIImageView中保留图像纵横比,如CISepiaTone
【发布时间】:2013-08-16 23:57:30
【问题描述】:

在 iOS 中,我的 UIImageView 是“Aspect Fit”,但是当我执行 CIFilter 类型时,例如"CISepiaTone", 我失去了原始图像的纵横比。图像被拉伸到整个 UIImageView 大小。

如何保持纵横比?

【问题讨论】:

  • 注意:我试过(来自stackoverflow.com/questions/7645454/…)+(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContext(newSize); [图像 drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();返回新图像; } 但有时它会将图像旋转 90 度...

标签: ios objective-c


【解决方案1】:

如果您使用 imageWithCIImage 创建过滤后的图像,那么您将无法获得正确的纵横比。尝试从过滤器输出 CIImage 创建 CGImageRef,然后从 CGImageRef 创建 UIImage。

例子:

// 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2020-12-11
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    相关资源
    最近更新 更多