我制作了一个UIImage Objective C类别来保存这个功能:
#import "UIImage+fixOrientation.h"
@implementation UIImage (fixOrientation)
- (UIImage *)fixOrientation
{
// No-op if the orientation is already correct
if (self.imageOrientation == UIImageOrientationUp) return self;
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;
switch (self.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, self.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
}
switch (self.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, self.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
CGImageGetBitsPerComponent(self.CGImage), 0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
CGContextConcatCTM(ctx, transform);
switch (self.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
break;
}
// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
@end
我最大限度地利用了我的知识,但出于使用原因,它不断出现这个错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage fixOrientation]: unrecognized selector sent to instance 0x1f0ce270'
在我的UIImage + fixOrientation.h文件中,我的代码如下所示:
#import <UIKit/UIKit.h>
@interface UIImage (fixOrientation)
- (UIImage *)fixOrientation;
@end
我的UIImage + fixOrientation.m文件包含我在开头发布的代码。我已经查看了项目目标构建部分中的其他链接器标志并提供了一些建议,但这没有帮助!老实说,我无法弄清楚为什么它找不到这个功能...
这也是我使用该功能的方式:
NSData *imageData = UIImageJPEGRepresentation([[imageManager pickerImage] fixOrientation], 0.5);
有没有人知道为什么它可能会把这个消息扔给我?提前谢谢!
答案 0 :(得分:9)
好像你只是忘了将该类别添加到目标中。
请查看该截图并检查它是否与您相同:
答案 1 :(得分:0)
您需要将#import UIImage+fixOrientation.h
添加到您希望使用该类别的任何文件中。或者,如果该方法通常对整个项目有帮助,您可以将其放在*-Prefix.pch
文件中,以便它可以在项目的任何地方使用。