图片旋转90度解决办法

简介: <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(85,85,85); font-family:'microsoft yahei'; font-size:15px; line-height:35px"> 如果把通过相机获取到的图片,直接进行操作, 比如裁剪

如果把通过相机获取到的图片,直接进行操作, 比如裁剪, 缩放, 则会把原图片向又旋转90度。


ps: 查找过程中, 碰到了一种说法:

[objc]  view plain copy
  1. //get original photo from iOS photos   
  2. //如果该图片大于2M,会自动旋转90度;否则不旋转  
  3. UIImage* originalImg=[dict objectForKey:UIImagePickerControllerOriginalImage];  

至于是否正确, 还没确定。 先Mark。


下面的解决办法亲测可行。 原文:http://www.cnblogs.com/jiangyazhou/archive/2012/03/22/2412343.html

用相机拍摄出来的照片含有EXIF信息,UIImage的imageOrientation属性指的就是EXIF中的orientation信息。
如果我们忽略orientation信息,而直接对照片进行像素处理或者drawInRect等操作,得到的结果是翻转或者旋转90之后的样子。这是因为我们执行像素处理或者drawInRect等操作之后,imageOrientaion信息被删除了,imageOrientaion被重设为0,造成照片内容和imageOrientaion不匹配。
所以,在对照片进行处理之前,先将照片旋转到正确的方向,并且返回的imageOrientaion为0。
下面这个方法就是一个UIImage category中的方法,用它可以达到以上目的。

[objc]  view plain copy
  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.width0);  
  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.width0);  
  37.             transform = CGAffineTransformScale(transform, -11);  
  38.             break;  
  39.               
  40.         case UIImageOrientationLeftMirrored:  
  41.         case UIImageOrientationRightMirrored:  
  42.             transform = CGAffineTransformTranslate(transform, aImage.size.height0);  
  43.             transform = CGAffineTransformScale(transform, -11);  
  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. }  
目录
相关文章
|
9天前
|
小程序
小程序消除图片下边距的三个方法
小程序消除图片下边距的三个方法
33 11
|
4月前
|
编解码 JavaScript 算法
通过PHAsset获取的图片上传后变大和图像被旋转90度问题完美解决方案
通过PHAsset获取的图片上传后变大和图像被旋转90度问题完美解决方案
83 4
|
4月前
采用SDAutoLayout布局的图片无法旋转45度问题及解决方案
采用SDAutoLayout布局的图片无法旋转45度问题及解决方案
28 0
|
4月前
|
监控 API 计算机视觉
OpenCV这么简单为啥不学——1.6、图像旋转与翻转(rotate函数、imutils环境安装、imutils任意角度旋转)
OpenCV这么简单为啥不学——1.6、图像旋转与翻转(rotate函数、imutils环境安装、imutils任意角度旋转)
67 0
|
4月前
|
监控 API 计算机视觉
OpenCV这么简单为啥不学——1.10、addWeighted设置图片透明度
OpenCV这么简单为啥不学——1.10、addWeighted设置图片透明度
61 0
|
10月前
|
Python
Pycharm 随时调整字体大小(放大或缩小)
Pycharm 随时调整字体大小(放大或缩小)
81 0
|
前端开发
css三角号旋转90度,上下移动动画效果demo效果(整理)
css三角号旋转90度,上下移动动画效果demo效果(整理)
|
数据采集 编解码 数据挖掘
【每周一坑】缩小图片尺寸
我们知道,通常来说一张图片的分辨率越高,它就越清晰,但文件占用的空间就越大。有时候我们并不需要那么高的清晰度,而是希望图片占用空间可以小一些。