使用 CoreImage 过滤图像会导致图像旋转

时间:2023-03-24
本文介绍了使用 CoreImage 过滤图像会导致图像旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

感谢阅读.

我正在尝试在 iOS 5 中使用 CoreImage 来更改图像的外观.问题是现有图像在此过程中似乎丢失了其方向信息并最终旋转了 90 度.

I'm trying to use CoreImage in iOS 5 to alter the appearance of an image. The problem is that the existing image appears to lose its orientation information during the process and ends up rotated by 90 degrees.

代码如下:

CIContext *context = [CIContext contextWithOptions:nil];
CIImage *img = [CIImage imageWithCGImage:imageView.image.CGImage];
CIFilter *adjustFilter = [CIFilter filterWithName:@"CIHighlightShadowAdjust"];

[adjustFilter setDefaults];
[adjustFilter setValue:img forKey:kCIInputImageKey];
[adjustFilter setValue:[NSNumber numberWithFloat:0.3] forKey:@"inputShadowAmount"];
CIImage *adjustedImage = [adjustFilter valueForKey:kCIOutputImageKey];
UIImage *newPtImage = [UIImage imageWithCGImage:[context adjustedImage fromRect:adjustedImage.extent]];

imageView.image = newPtImage;

原始图像通常来自相机拍摄的图像,但并非总是如此.

The original image generally comes from one taken by the camera, but not always.

希望有人能提供帮助.

罗伯.

推荐答案

其实我的问题的解决方案很简单.由于 CGImage 失去了它的方向和比例因子,我只需要添加

Actually, the solution to my issue turned out to be quite simple. Since the CGImage loses its orientation and scale factors, I simply needed to add

UIImageOrientation originalOrientation = imageView.image.imageOrientation;
CGFloat originalScale = imageView.image.scale;

在代码的早期,然后用这个代替 [UIImage imageWithCGImage:]

early in the code and then used this instead of [UIImage imageWithCGImage:]

    UIImage *newPtImage = [UIImage imageWithCGImage:img2 scale:originalScale orientation:originalOrientation];

也感谢您的建议.

这篇关于使用 CoreImage 过滤图像会导致图像旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:iOS5 TabBar 字体和颜色 下一篇:iOS 中的访问设置应用程序

相关文章