UIImage sideway after converting it from CIImage

…衆ロ難τιáo~ 提交于 2019-12-11 10:17:22

问题


I'm applying filter on UIImage but after conversion, it appear as sideway. Below is my code.

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];

It renders successfully in newImage, and dimensions, but the image is side way. Is there any place in above code that cause orientation change ?


回答1:


Scale the image to proper orientation before applying filter.

follow this link: https://stackoverflow.com/q/538064/871102

Instead of:

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

write:

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

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



回答2:


Yes, you may consider setting the image orientation to be sure of one initializing the UIImage with method:

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

And you have couple of image orientations:

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;

The Up is default - so try to rotate it.



来源:https://stackoverflow.com/questions/20657131/uiimage-sideway-after-converting-it-from-ciimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!