8

我正在使用 UIImagePickerController 在 iPhone 上以纵向模式拍摄照片并保存到网络上。照片在手机上以纵向显示,但在网络上旋转 90 度。

如果我下载照片并在 Preview (mac) 或 Photoshop (mac or pc) 中查看它,它又是纵向的。在 Windows 图片查看器 (pc) 中,它旋转为横向。

在上传之前我需要对图像数据应用旋转变换吗?然后我是否还需要删除在 Photoshop 和预览中旋转它的元数据?

4

4 回答 4

7

问题是图像旋转被添加到照片中作为大多数浏览器不使用的 EXIF 数据。有两种解决方案:

  1. 在服务器端应用轮换。我使用的是 Ruby 插件 Paperclip(由 Thoughtbot 提供),只需在模型中的 has_attached_file 命令中包含 auto-orient convert 选项:

    has_attached_file :photo, :convert_options => { :all => '-auto-orient' }

  2. 在 iPhone 应用程序中旋转照片。这在另一个 stackoverflow 问题中得到了解决;感谢@Squeegy,调用scaleAndRotate 方法将旋转元数据替换为图像变换。

于 2009-06-26T20:39:33.957 回答
3

这是 CarrierWavew/MiniMagick 的解决方案:http: //carrierwave.rubyforge.org/rdoc/classes/CarrierWave/MiniMagick.html

于 2011-05-15T15:22:28.643 回答
0

在将图像上传到服务器之前拍摄照片后,只需将拍摄的图像传递给一个方法,它肯定对您有用

#pragma mark Rotate
- (UIImage *)scaleAndRotateImage:(UIImage *)image {
    int kMaxResolution = 640; // Or whatever

    CGImageRef imgRef = image.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);


    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
    if (width > kMaxResolution || height > kMaxResolution) {
        CGFloat ratio = width/height;
        if (ratio > 1) {
            bounds.size.width = kMaxResolution;
            bounds.size.height = roundf(bounds.size.width / ratio);
        }
        else {
            bounds.size.height = kMaxResolution;
            bounds.size.width = roundf(bounds.size.height * ratio);
        }
    }

    CGFloat scaleRatio = bounds.size.width / width;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient = image.imageOrientation;
    switch(orient) {

        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;

        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;

        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;

        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];

    }

    UIGraphicsBeginImageContext(bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
        CGContextScaleCTM(context, -scaleRatio, scaleRatio);
        CGContextTranslateCTM(context, -height, 0);
    }
    else {
        CGContextScaleCTM(context, scaleRatio, -scaleRatio);
        CGContextTranslateCTM(context, 0, -height);
    }

    CGContextConcatCTM(context, transform);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageCopy;
}
于 2014-02-07T07:27:15.790 回答
-1

如果图像在 MS Windows 中以正确的方向保存,这是手动覆盖 EXIF 旋转元数据的简单方法。在 Windows 资源管理器中,右键单击图像文件并选择“顺时针旋转”。这样做 4 次以将图像一直旋转,然后图像将具有所有系统的正确方向。然后您可以将图像上传到您的网络服务器。

于 2015-10-26T20:22:36.660 回答