0

我制作了一个 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);

有谁知道为什么它可能会向我抛出这条消息?提前致谢!

4

2 回答 2

9

似乎您只是忘记将该类别添加到目标中。

请查看该屏幕截图并检查它是否对您来说看起来相同:

在此处输入图像描述

于 2012-12-04T15:49:32.927 回答
0

您将需要添加#import UIImage+fixOrientation.h到您希望在其中使用该类别的任何文件中。或者,如果该方法通常对整个项目有帮助,您可以将它放在您的*-Prefix.pch文件中,以便在项目中的任何地方都可以使用它。

于 2012-12-04T15:54:20.460 回答