Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone摄像头图像上传到web时会旋转_Iphone_Uiimagepickercontroller_Image Uploading - Fatal编程技术网

iPhone摄像头图像上传到web时会旋转

iPhone摄像头图像上传到web时会旋转,iphone,uiimagepickercontroller,image-uploading,Iphone,Uiimagepickercontroller,Image Uploading,我正在使用UIImagePickerController在iphone上以纵向模式拍照并保存到web。照片在手机上以纵向显示,但在网络上旋转90度 如果我下载照片并在预览(mac)或Photoshop(mac或pc)中查看它,它将再次处于纵向。在Windows图片查看器(pc)中,它旋转到横向 上传前是否需要对图像数据应用旋转变换?然后,我是否还需要删除在Photoshop和预览中旋转的元数据?问题是,图像旋转作为大多数浏览器未使用的EXIF数据添加到照片中。有两种解决方案: 在服务器端应用旋转

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

如果我下载照片并在预览(mac)或Photoshop(mac或pc)中查看它,它将再次处于纵向。在Windows图片查看器(pc)中,它旋转到横向


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

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

  • 在服务器端应用旋转。我当时使用的是Ruby插件曲别针(Thoughtbot),只需在模型中的has_attached_file命令中包含自动定向转换选项:

    已附加文件:照片,:转换选项=>{:全部=>'-自动定向'}

  • 在iPhone应用程序中旋转照片。这在另一个stackoverflow问题中得到了解决;由于@squegy,调用将用图像变换替换旋转元数据


  • 以下是CarrierWavew/MiniMagick的解决方案:

    在将图像上传到服务器之前,一旦您拍摄了照片,只需将拍摄的图像传递到一个方法中,它肯定会对您有效

    #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;
    }
    

    如果在MS Windows中以正确的方向保存图像,那么这里有一种手动覆盖EXIF旋转元数据的简单方法。在Windows资源管理器中,右键单击图像文件并选择“顺时针旋转”。执行此操作4次以将图像旋转到底,然后图像将具有适用于所有系统的正确方向。然后,您可以将图像上载到web服务器。

    很好!感谢您及时更新答案。我相信您的评论是错误的。例如UIImageOrientationUp://EXIF=0