【问题标题】:Using CoreImage to filter an image results in image rotation使用 CoreImage 过滤图像会导致图像旋转
【发布时间】:2011-12-25 21:52:22
【问题描述】:

感谢阅读。

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

代码如下:

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;

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

希望有人能提供帮助。

罗伯。

【问题讨论】:

    标签: objective-c ios5 core-image


    【解决方案1】:

    看看这个答案https://stackoverflow.com/q/538064/871102

    您可以将图像传递给上一个答案的方法来解决您的问题。所以而不是:

    CIImage *img = [CIImage imageWithCGImage:imageView.image.CGImage];
    

    写:

    UIImage *fixedImage = [self scaleAndRotateImage:imageView.image];
    CIImage *img = [CIImage imageWithCGImage:fixedImage.CGImage];
    

    更新:

    这里有一个没有图像缩放的更优雅的答案:iOS UIImagePickerController result image orientation after upload

    【讨论】:

    • 这不起作用。它运行了代码,但什么也没发生。而且它太长而且很hacky。
    • 在 Swift 中会怎么样?请有人告诉我!
    • 实际解决方案如下
    【解决方案2】:

    实际上,我的问题的解决方案非常简单。由于 CGImage 失去了它的方向和比例因子,我只需要添加

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

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

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

    也感谢您的建议。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多