问题
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