我们上传图片到服务器原以为就是我们所看的预览效果一样是正面显示的,但是当我们下载图片后,却发现图片发生了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;
}