简体   繁体   中英

UIImage sideway after converting it from CIImage

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 ?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM