调用UIImageJPEGRepresentation时的UIImage方向问题

时间:2013-06-20 10:30:09

标签: uiimage uiimagejpegrepresentation

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

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

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

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

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会影响方向?

提前致谢。

1 个答案:

答案 0 :(得分:-1)

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

现在如何实际添加,我不知道......从How to write orientation data to the UIImage iOS SDKProblem setting exif data for an image以及iOS UIImagePickerController result image orientation after upload

开始