Objective c 使用CoreImage过滤图像会导致图像旋转
谢谢你的阅读 我试图在iOS 5中使用CoreImage来改变图像的外观。问题在于,现有图像在处理过程中似乎丢失了方向信息,最终旋转了90度 代码如下:Objective c 使用CoreImage过滤图像会导致图像旋转,objective-c,ios5,core-image,Objective C,Ios5,Core Image,谢谢你的阅读 我试图在iOS 5中使用CoreImage来改变图像的外观。问题在于,现有图像在处理过程中似乎丢失了方向信息,最终旋转了90度 代码如下: CIContext *context = [CIContext contextWithOptions:nil]; CIImage *img = [CIImage imageWithCGImage:imageView.image.CGImage]; CIFilter *adjustFilter = [CIFilter filterWithName
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;
原始图像通常来自相机拍摄的图像,但并不总是如此
希望有人能帮忙
罗布。看看这个答案 您可以通过将图像传递到上一个答案的方法来解决问题。因此,不是:
CIImage *img = [CIImage imageWithCGImage:imageView.image.CGImage];
写:
UIImage *fixedImage = [self scaleAndRotateImage:imageView.image];
CIImage *img = [CIImage imageWithCGImage:fixedImage.CGImage];
更新:
这里有一个没有图像缩放的更优雅的答案:。事实上,我的问题的解决方案非常简单。由于CGImage失去了方向和比例因子,我只需要添加
UIImageOrientation originalOrientation = imageView.image.imageOrientation;
CGFloat originalScale = imageView.image.scale;
在代码的早期,然后使用它代替[UIImage imageWithCGImage:]
UIImage *newPtImage = [UIImage imageWithCGImage:img2 scale:originalScale orientation:originalOrientation];
也谢谢你的建议。这没用。它运行了代码,但什么也没发生。而且它太长了,而且很粗糙。在Swift里会是什么样子?请有人告诉我!实际解决方案如下