从 iphone 设备上传图像时出现图像方向问题

Posted

技术标签:

【中文标题】从 iphone 设备上传图像时出现图像方向问题【英文标题】:Image orientation issue while uploading the image from the iphone device 【发布时间】:2014-05-07 11:08:51 【问题描述】:

我想上传从设备库中拍摄的图像。图像是从设备以portrait 模式拍摄的。但是在将该图像上传到服务器图像后,它会旋转 90 度。

我还使用了旋转图像的代码。当我使用UIImagePicker 拍照时,图像会在不改变方向的情况下上传。但是当我尝试从相机胶卷上传图像时,portrait 图像上传并以landscape 模式显示。

还有谁能告诉我ipad/ipod?的图片分辨率是多少

请有人帮助我。

这是我的代码:

//int kMaxResolution = 320; // Or whatever
int kMaxResolution = image.size.width;

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 = bounds.size.width / ratio;
    
    else 
        bounds.size.height = kMaxResolution;
        bounds.size.width = 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();

// [self setRotatedImage:imageCopy];
return imageCopy;

这非常适合UIImagePicker 的东西。但是从相机胶卷上传肖像图像时失败。

【问题讨论】:

试试这个 UIImage 类别.. 它会帮助你.. vocaro.com/trevor/blog/wp-content/uploads/2009/10/… 【参考方案1】:

我也遇到过同样的问题,但解决方案不是来自我们的本机代码,解决方案应该从服务器端完成,即。当服务器收到图像时,如果我们在上传之前使用相同的图像,您必须尝试像我们的代码一样旋转该图像,这样就可以了,旋转发生在将其上传到服务器时。

【讨论】:

是的,我们可以,但 id 取决于服务器上编写的代码来接收图像。

以上是关于从 iphone 设备上传图像时出现图像方向问题的主要内容,如果未能解决你的问题,请参考以下文章

读取 JPEG 元数据(方向)时出现问题

保存用相机拍摄或从相机胶卷中选择的图像时出现长时间延迟 - iPhone

仅在 iOS 移动 Safari 中上传图像时出现错误的 CORS 失败

从 React 前端将图像上传到烧瓶后端时出现 KeyError 'image'

iPhone OS 图像/照片大小调整工具更改 EXIF 方向数据

在设备上显示图像时出现问题