从CIImage转换后,UIImage横向移动

时间:2013-12-18 11:37:36

标签: ios iphone cocoa-touch core-graphics cgcontext

我正在对UIImage应用过滤器,但在转换后,它显示为横向。以下是我的代码。

CIImage *ciImage = [[CIImage alloc] initWithImage:self.imgToProcess];

CIFilter *filter = [CIFilter filterWithName:@"CIPhotoEffectChrome"
                                              keysAndValues:kCIInputImageKey, ciImage, nil];
[filter setDefaults];

CIContext *context = [CIContext contextWithOptions:nil];
CIImage *outputImage = [filter outputImage];

CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]];

UIImage *newImage = [[UIImage alloc]initWithCIImage:outputImage];

它在newImage和维度中成功渲染,但图像是侧面的。上面的代码中是否有任何地方会导致方向改变?

2 个答案:

答案 0 :(得分:1)

在应用滤镜之前将图像缩放到正确的方向。

点击此链接:https://stackoverflow.com/q/538064/871102

而不是:

CIImage *ciImage = [[CIImage alloc] initWithImage:self.imgToProcess];

写:

UIImage *properOrientedImage = [self scaleAndRotateImage:self.imgToProcess];

CIImage *ciImage = [[CIImage alloc] initWithImage:properOrientedImage.CGImage];

答案 1 :(得分:0)

是的,您可以考虑设置图像方向,以确保使用方法初始化UIImage

- (id)initWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation

你有几个图像方向:

typedef enum {
   UIImageOrientationUp,
   UIImageOrientationDown,   // 180 deg rotation
   UIImageOrientationLeft,   // 90 deg CW
   UIImageOrientationRight,   // 90 deg CCW
   UIImageOrientationUpMirrored,    // as above but image mirrored along
   // other axis. horizontal flip
   UIImageOrientationDownMirrored,  // horizontal flip
   UIImageOrientationLeftMirrored,  // vertical flip
   UIImageOrientationRightMirrored, // vertical flip
} UIImageOrientation;

Up是默认值 - 因此请尝试旋转它。