iOS开发- 相机(摄像头)获取到的图片自动旋转90度解决办法
今天要做一个读取摄像头图片功能,发现拍摄之后的图片会在拍摄水平的基础上自动发生90°旋转。通过下面的方法 确实修正了,未避免选照片也发生该案例,我觉得在选取图片的时候也要进行修正。
//get original photo from iOS photos //如果该图片大于2M,会自动旋转90度;否则不旋转
网络上说:用相机拍摄出来的照片含有EXIF信息,UIImage的imageOrientation属性指的就是EXIF中的orientation信息。如果我们忽略orientation信息,而直接对照片进行像素处理或者drawInRect等操作,得到的结果是翻转或者旋转90之后的样子。这是因为我们执行像素处理或者drawInRect等操作之后,imageOrientaion信息被删除了,imageOrientaion被重设为0,造成照片内容和imageOrientaion不匹配。所以,在对照片进行处理之前,先将照片旋转到正确的方向,并且返回的imageOrientaion为0。
修正代码如下:
1 | – (UIImage *)fixOrientation:(UIImage *)aImage { |
2 | |
3 | // No-op if the orientation is already correct |
4 | if (aImage.imageOrientation == UIImageOrientationUp) |
5 | return aImage; |
6 | |
7 | // We need to calculate the proper transformation to make the image upright. |
8 | // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. |
9 | CGAffineTransform transform = CGAffineTransformIdentity; |
10 | |
11 | switch (aImage.imageOrientation) { |
12 | case UIImageOrientationDown: |
13 | case UIImageOrientationDownMirrored: |
14 | transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height); |
15 | transform = CGAffineTransformRotate(transform, M_PI); |
16 | break; |
17 | |
18 | case UIImageOrientationLeft: |
19 | case UIImageOrientationLeftMirrored: |
20 | transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); |
21 | transform = CGAffineTransformRotate(transform, M_PI_2); |
22 | break; |
23 | |
24 | case UIImageOrientationRight: |
25 | case UIImageOrientationRightMirrored: |
26 | transform = CGAffineTransformTranslate(transform, 0, aImage.size.height); |
27 | transform = CGAffineTransformRotate(transform, -M_PI_2); |
28 | break; |
29 | default: |
30 | break; |
31 | } |
32 | |
33 | switch (aImage.imageOrientation) { |
34 | case UIImageOrientationUpMirrored: |
35 | case UIImageOrientationDownMirrored: |
36 | transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); |
37 | transform = CGAffineTransformScale(transform, -1, 1); |
38 | break; |
39 | |
40 | case UIImageOrientationLeftMirrored: |
41 | case UIImageOrientationRightMirrored: |
42 | transform = CGAffineTransformTranslate(transform, aImage.size.height, 0); |
43 | transform = CGAffineTransformScale(transform, -1, 1); |
44 | break; |
45 | default: |
46 | break; |
47 | } |
48 | |
49 | // Now we draw the underlying CGImage into a new context, applying the transform |
50 | // calculated above. |
51 | CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height, |
52 | CGImageGetBitsPerComponent(aImage.CGImage), 0, |
53 | CGImageGetColorSpace(aImage.CGImage), |
54 | CGImageGetBitmapInfo(aImage.CGImage)); |
55 | CGContextConcatCTM(ctx, transform); |
56 | switch (aImage.imageOrientation) { |
57 | case UIImageOrientationLeft: |
58 | case UIImageOrientationLeftMirrored: |
59 | case UIImageOrientationRight: |
60 | case UIImageOrientationRightMirrored: |
61 | // Grr… |
62 | CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage); |
63 | break; |
64 | |
65 | default: |
66 | CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage); |
67 | break; |
68 | } |
69 | |
70 | // And now we just create a new UIImage from the drawing context |
71 | CGImageRef cgimg = CGBitmapContextCreateImage(ctx); |
72 | UIImage *img = [UIImage imageWithCGImage:cgimg]; |
73 | CGContextRelease(ctx); |
74 | CGImageRelease(cgimg); |
75 | return img; |
76 | } |