iOS_上传服务器的图片发生旋转的问题

我们上传图片到服务器原以为就是我们所看的预览效果一样是正面显示的,但是当我们下载图片后,却发现图片发生了90°旋转.这是怎么回事呢?下面我们来看看这是怎么一回事.

图片我们用的是拍照的图片:

当是竖着拍是,预览图片是正的,但是上传服务器后会发生90°旋转


竖着拍的照片
发生了旋转

上传服务器前的图片信息:

{
    UIImagePickerControllerMediaMetadata =     {
        DPIHeight = 72;
        DPIWidth = 72;
        Orientation = 6;
        kCGImageDestinationICCProfile = <00000224 6170706c 04000000 6d6e7472 52474220 58595a20 07e10007 0007000d 00160020 61637370 4150504c 00000000 4150504c 00000000 00000000 00000000 00000000 0000f6d6 00010000 0000d32d 6170706c ca1a9582 257f104d 389913d5 d1ea1582 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0000000a 64657363 000000fc 00000065 63707274 00000164 00000023 77747074 00000188 00000014 7258595a 0000019c 00000014 6758595a 000001b0 00000014 6258595a 000001c4 00000014 72545243 000001d8 00000020 63686164 000001f8 0000002c 62545243 000001d8 00000020 67545243 000001d8 00000020 64657363 00000000 0000000b 44697370 6c617920 50330000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 74657874 00000000 436f7079 72696768 74204170 706c6520 496e632e 2c203230 31370000 58595a20 00000000 0000f351 00010000 000116cc 58595a20 00000000 000083df 00003dbf ffffffbb 58595a20 00000000 00004abf 0000b137 00000ab9 58595a20 00000000 00002838 0000110b 0000c8b9 70617261 00000000 00030000 00026666 0000f2a7 00000d59 000013d0 00000a5b 73663332 00000000 00010c42 000005de fffff326 00000793 0000fd90 fffffba2 fffffda3 000003dc 0000c06e>;
        "{Exif}" =         {
            ApertureValue = "1.6959938131099";
            BrightnessValue = "4.200180446892688";
            ColorSpace = 65535;
            DateTimeDigitized = "2018:06:26 11:17:09";
            DateTimeOriginal = "2018:06:26 11:17:09";
            ExposureBiasValue = "0.1749256825006788";
            ExposureMode = 0;
            ExposureProgram = 2;
            ExposureTime = "0.0303030303030303";
            FNumber = "1.8";
            Flash = 24;
            FocalLenIn35mmFilm = 28;
            FocalLength = "3.99";
            ISOSpeedRatings =             (
                25
            );
            LensMake = Apple;
            LensModel = "iPhone 8 Plus back dual camera 3.99mm f/1.8";
            LensSpecification =             (
                "3.99",
                "6.6",
                "1.8",
                "2.8"
            );
            MeteringMode = 3;
            PixelXDimension = 4032;
            PixelYDimension = 3024;
            SceneType = 1;
            SensingMethod = 2;
            ShutterSpeedValue = "5.059855806488952";
            SubjectArea =             (
                2577,
                1100,
                753,
                756
            );
            SubsecTimeDigitized = 810;
            SubsecTimeOriginal = 810;
            WhiteBalance = 0;
        };
        "{MakerApple}" =         {
            1 = 9;
            12 =             (
                "29.6328125",
                "2.33203125"
            );
            13 = 3;
            14 = 0;
            15 = 2;
            16 = 1;
            2 = <84009100 97008900 aa00df00 1701a800 b300b100 aa00a800 a700a500 a500a300 86009500 9800dd00 09010b01 0a01a900 b600af00 a900a500 a700a500 a900a600 89009800 9a00e100 0101f700 0301aa00 b800ae00 a600a400 a600aa00 ab00a800 8b009b00 9b00e200 0201f900 1201aa00 b700ae00 96009600 a300aa00 ad00a900 8d009d00 9d00de00 1201f500 0a01a700 bb00a100 6c005f00 8400aa00 af00ad00 8f00a000 9f00e200 07010301 1e01af00 bf008c00 87007400 5c00a400 b200b100 9000a300 a100e800 0e010901 0d01b400 ba00a300 ad009600 8100ab00 b500b300 9200a500 a400ec00 15010701 2601c200 b400e300 d800c800 b100b700 b900b700 9500a800 a700eb00 0f011401 3c01c400 dc002301 19010601 ca00c100 ba00ba00 9800a900 a800f400 18011201 3501ca00 27015f01 51013101 c600c400 be00be00 9a00ac00 ab000501 30011601 3a01d000 8d019e01 8a012201 d000c700 c200c100 9900b000 ac000501 1e013401 4201d800 d801f001 c601ee00 d100c900 c600c800 9800b200 ae00ff00 20012601 3901dd00 68017402 ce01da00 d100ce00 cb00cc00 9700b200 b0000801 2a013001 4a01d900 db007901 2701e100 d300d100 d200ce00 9400b200 b3000f01 27013201 3b01de00 dc00e400 e400e200 db00d700 d700d100 9100ae00 b5001401 2d012f01 3f01e400 e300e700 eb00e500 e200dc00 da00d500>;
            20 = 5;
            23 = 0;
            25 = 0;
            3 =             {
                epoch = 0;
                flags = 1;
                timescale = 1000000000;
                value = 1091865810293208;
            };
            31 = 0;
            4 = 0;
            5 = 180;
            6 = 191;
            7 = 1;
            8 =             (
                "0.01527284272015095",
                "-0.4884061813354492",
                "-0.8795660734176636"
            );
        };
        "{TIFF}" =         {
            DateTime = "2018:06:26 11:17:09";
            Make = Apple;
            Model = "iPhone 8 Plus";
            ResolutionUnit = 2;
            Software = "11.2.5";
            XResolution = 72;
            YResolution = 72;
        };
    };
    UIImagePickerControllerMediaType = "public.image";
    UIImagePickerControllerOriginalImage = "<UIImage: 0x1cc2a0cc0> size {3024, 4032} orientation 3 scale 1.000000";
}

当是横着拍时,预览图片也是正的,上传服务器后没有发生旋转.


横着拍的照片
没有发生旋转

上传服务器前的图片信息:

{
    UIImagePickerControllerMediaMetadata =     {
        DPIHeight = 72;
        DPIWidth = 72;
        Orientation = 1;
        kCGImageDestinationICCProfile = <00000224 6170706c 04000000 6d6e7472 52474220 58595a20 07e10007 0007000d 00160020 61637370 4150504c 00000000 4150504c 00000000 00000000 00000000 00000000 0000f6d6 00010000 0000d32d 6170706c ca1a9582 257f104d 389913d5 d1ea1582 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0000000a 64657363 000000fc 00000065 63707274 00000164 00000023 77747074 00000188 00000014 7258595a 0000019c 00000014 6758595a 000001b0 00000014 6258595a 000001c4 00000014 72545243 000001d8 00000020 63686164 000001f8 0000002c 62545243 000001d8 00000020 67545243 000001d8 00000020 64657363 00000000 0000000b 44697370 6c617920 50330000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 74657874 00000000 436f7079 72696768 74204170 706c6520 496e632e 2c203230 31370000 58595a20 00000000 0000f351 00010000 000116cc 58595a20 00000000 000083df 00003dbf ffffffbb 58595a20 00000000 00004abf 0000b137 00000ab9 58595a20 00000000 00002838 0000110b 0000c8b9 70617261 00000000 00030000 00026666 0000f2a7 00000d59 000013d0 00000a5b 73663332 00000000 00010c42 000005de fffff326 00000793 0000fd90 fffffba2 fffffda3 000003dc 0000c06e>;
        "{Exif}" =         {
            ApertureValue = "1.6959938131099";
            BrightnessValue = "4.475249917646829";
            ColorSpace = 65535;
            DateTimeDigitized = "2018:06:26 11:23:21";
            DateTimeOriginal = "2018:06:26 11:23:21";
            ExposureBiasValue = 0;
            ExposureMode = 0;
            ExposureProgram = 2;
            ExposureTime = "0.0303030303030303";
            FNumber = "1.8";
            Flash = 24;
            FocalLenIn35mmFilm = 28;
            FocalLength = "3.99";
            ISOSpeedRatings =             (
                25
            );
            LensMake = Apple;
            LensModel = "iPhone 8 Plus back dual camera 3.99mm f/1.8";
            LensSpecification =             (
                "3.99",
                "6.6",
                "1.8",
                "2.8"
            );
            MeteringMode = 5;
            PixelXDimension = 4032;
            PixelYDimension = 3024;
            SceneType = 1;
            SensingMethod = 2;
            ShutterSpeedValue = "5.059855806488952";
            SubjectArea =             (
                2015,
                1511,
                2217,
                1330
            );
            SubsecTimeDigitized = 480;
            SubsecTimeOriginal = 480;
            WhiteBalance = 0;
        };
        "{MakerApple}" =         {
            1 = 9;
            12 =             (
                "0.83984375",
                "45.84765625"
            );
            13 = 6;
            14 = 0;
            15 = 2;
            16 = 1;
            2 = <7e008d00 98009d00 9f009e00 9c009900 96009300 91008d00 8b008700 84007f00 97009e00 a100a100 9c009a00 96009500 93009100 8f008d00 8c008a00 86008400 af000501 fe00f200 e900da00 cf00c700 bd00b200 ab00a200 99009000 88008100 e0001701 0c010101 f800ec00 e600e000 d800d800 d500d500 d300d100 ce009700 0c013f01 0501fe00 fa00f800 ef00ea00 e500e000 df00dc00 db00db00 dc00b700 2c013801 04010101 fb00f400 f100ec00 ee00ea00 df00d900 de00dc00 d900d900 44013501 0d010601 0301fe00 ed00ea00 ee00ee00 dd00db00 dc00e100 f2000101 31013301 0c010401 fe000c01 ef00ee00 f400f800 fd00fb00 0601f300 eb00ed00 18012301 2b011301 08013501 30012201 12010701 e400eb00 d400d800 d000d000 c700b800 a300a400 a0009e00 97009600 97009b00 95009900 9800a500 b100ab00 d200c700 c300bb00 b600b100 af00ae00 ab00a500 a3009900 90008c00 87008200 d000c900 bd00b700 b100ac00 ac00aa00 a700a500 a600a500 a1009c00 9f009b00 d200cb00 c000bc00 b300ad00 aa00a600 a200a100 9d00a000 a3009d00 9b009b00 d200c800 c400bb00 b800ae00 aa00a500 a200a200 9f009e00 9a009a00 99009800 d700ca00 c500bd00 b500b100 a900a600 a200a100 9f009a00 99009800 97009400 d800cc00 c500bb00 b600b400 ac00a700 a500a000 9f009d00 9c009900 94009300>;
            20 = 5;
            23 = 0;
            25 = 0;
            3 =             {
                epoch = 0;
                flags = 1;
                timescale = 1000000000;
                value = 1092237498460958;
            };
            31 = 0;
            4 = 1;
            5 = 192;
            6 = 198;
            7 = 1;
            8 =             (
                "-0.5642696022987366",
                "0.04399513080716133",
                "-0.8285016417503357"
            );
        };
        "{TIFF}" =         {
            DateTime = "2018:06:26 11:23:21";
            Make = Apple;
            Model = "iPhone 8 Plus";
            ResolutionUnit = 2;
            Software = "11.2.5";
            XResolution = 72;
            YResolution = 72;
        };
    };
    UIImagePickerControllerMediaType = "public.image";
    UIImagePickerControllerOriginalImage = "<UIImage: 0x1c02aa020> size {4032, 3024} orientation 0 scale 1.000000";
}

从上面的图片信息,我们发现了一个属性Orientation,查找得

typedef NS_ENUM(NSInteger, UIImageOrientation) {
    UIImageOrientationUp,            // default orientation
    UIImageOrientationDown,          // 180 deg rotation
    UIImageOrientationLeft,          // 90 deg CCW
    UIImageOrientationRight,         // 90 deg CW
    UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip
    UIImageOrientationDownMirrored,  // horizontal flip
    UIImageOrientationLeftMirrored,  // vertical flip
    UIImageOrientationRightMirrored, // vertical flip
};

针对UIImageOrientation,查找了各方面的资料,终于找到解决的方法:
方法一:

- (UIImage *)fixOrientationWithImage:(UIImage *)image {
    
    // No-op if the orientation is already correct
    if (image.imageOrientation == UIImageOrientationUp) return image;
    
    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    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;
}

方法二:

    if (image.imageOrientation != UIImageOrientationUp)
    {
        UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
        [image drawInRect:(CGRect){0,0,image.size}];
        UIImage * normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        image = normalizedImage;
    }

参考:

iOS_UIImage的方向(imageOrientation)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 201,312评论 5 473
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,578评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,337评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,134评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,161评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,303评论 1 280
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,761评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,421评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,609评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,450评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,504评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,194评论 3 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,760评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,836评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,066评论 1 257
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,612评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,178评论 2 341

推荐阅读更多精彩内容