0

我有一个来自相机胶卷的 UIImage。我有以下代码:

UIImage *img = [UIImage imageWithCGImage: [[asset defaultRepresentation] fullResolutionImage]];
return UIImageJPEGRepresentation(img, 1.0);

当我在 UIImageView 中查看 img 时,方向是正确的。但是,在我从 UIImageJPEGRepresentation 返回 NSData 对象后,一些图像会出现颠倒(不是所有图像,只是其中一些图像)。

我找到了应该修复它的代码,但它对我不起作用,因为出现颠倒的代码会返回向上的方向,因此代码不会执行。我尝试使用的代码是:

if (image.imageOrientation == UIImageOrientationUp) return image;
CGAffineTransform transform = CGAffineTransformIdentity;

switch (image.imageOrientation) {
    case UIImageOrientationDown:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;

    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        break;

    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, image.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationUpMirrored:
        break;
}

switch (image.imageOrientation) {
    case UIImageOrientationUpMirrored:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.width, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;

    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, image.size.height, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
    case UIImageOrientationUp:
    case UIImageOrientationDown:
    case UIImageOrientationLeft:
    case UIImageOrientationRight:
        break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
                                         CGImageGetBitsPerComponent(image.CGImage), 0,
                                         CGImageGetColorSpace(image.CGImage),
                                         CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        // Grr...
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
        break;

    default:
        CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.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;

任何想法为什么 UIImageJPEGRepresentation 会影响方向?

提前致谢。

4

1 回答 1

-1

因为 UIImage 的 UIImageJPEGRepresentation 丢弃了大部分 UIImage 元数据,包括 imageOrientation 信息。您必须在转换为 JPEG 之后但在存储图像之前重新添加方向信息。否则,您的所有 JPEG 文件都会报告它们已定向向上。

现在如何实际添加,我不知道...从如何将方向数据写入 UIImage iOS SDK问题设置图像的 exif 数据上传后的 iOS UIImagePickerController 结果图像方向开始

于 2013-07-02T16:02:19.767 回答