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

- (UIImage *)fixOrientation:(UIImage *)aImage {// No-op if the orientation is already correctif (aImage.imageOrientation == UIImageOrientationUp) return aImage;// 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 (aImage.imageOrientation) {case UIImageOrientationDown:case UIImageOrientationDownMirrored:transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);transform = CGAffineTransformRotate(transform, M_PI);break;case UIImageOrientationLeft:case UIImageOrientationLeftMirrored:transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);transform = CGAffineTransformRotate(transform, M_PI_2);break;case UIImageOrientationRight:case UIImageOrientationRightMirrored:transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);transform = CGAffineTransformRotate(transform, -M_PI_2);break;default:break;}switch (aImage.imageOrientation) {case UIImageOrientationUpMirrored:case UIImageOrientationDownMirrored:transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);transform = CGAffineTransformScale(transform, -1, 1);break;case UIImageOrientationLeftMirrored:case UIImageOrientationRightMirrored:transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);transform = CGAffineTransformScale(transform, -1, 1);break;default:break;}// Now we draw the underlying CGImage into a new context, applying the transform// calculated above.CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,CGImageGetBitsPerComponent(aImage.CGImage), 0,CGImageGetColorSpace(aImage.CGImage),CGImageGetBitmapInfo(aImage.CGImage));CGContextConcatCTM(ctx, transform);switch (aImage.imageOrientation) {case UIImageOrientationLeft:case UIImageOrientationLeftMirrored:case UIImageOrientationRight:case UIImageOrientationRightMirrored:// Grr...CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);break;default:CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);break;}// And now we just create a new UIImage from the drawing contextCGImageRef cgimg = CGBitmapContextCreateImage(ctx);UIImage *img = [UIImage imageWithCGImage:cgimg];CGContextRelease(ctx);CGImageRelease(cgimg);return img;
}
原文地址: http://www.cnblogs.com/jiangyazhou/archive/2012/03/22/2412343.html

iPhone上关于相机拍照的图片反转90度的问题相关推荐

  1. 解决三星调用系统相机拍照显示图片旋转90度横着的问题

    最近项目有个功能是调用系统相机拍照上传图片的功能,发现别的手机都没有ok,只有三星的显示图片很明显是旋转了90度,横着的.后来百度了解是三星对android相机单独做了优化(android碎片化,各种 ...

  2. android 三星手机拍照旋转90度,解决三星调用系统相机拍照显示图片旋转90度横着的问题...

    /** * 调用系统相机拍照工具类 * @author yao * */ public class CaremaUtil { private static String strImgPath = &q ...

  3. android intent拍照,Android通过Intent方式调用相机拍照取得图片

    Android通过Intent方式调用相机拍照取得图片 AndroidManifest.XML 权限设置: XML布局设置: 代码: public classMainActivityextendsAp ...

  4. android 调用相机并获取图片地址,Android 7.0使用FileProvider获取相机拍照的图片路径...

    这里主要是基于Android 7.0,Nougat 实现一个获取相机拍照的图片后,使用FileProvider把图片转换为实际的路径. 首先需要在AndroidManifest.xml声明调用相机的权 ...

  5. Android自定义相机拍照、图片裁剪的实现

    原文:Android自定义相机拍照.图片裁剪的实现 最近项目里面又要加一个拍照搜题的功能,也就是用户对着不会做的题目拍一张照片,将照片的文字使用ocr识别出来,再调用题库搜索接口搜索出来展示给用户,类 ...

  6. 小米部分手机在相册或相机拍照裁剪图片时,出现错误问题

    小米部分手机在相册或相机拍照裁剪图片时,出现错误问题 Unable to load resource 0x00000000 from pkg=com.android.systemui 报错型号:Mi ...

  7. Unity for IOS 加载手机相册图片以及打开相机拍照获取图片

    Unity for IOS 加载手机相册图片以及打开相机拍照获取图片 最近想做一个使用unity for IOS获取手机图片的功能,所以就研究了一下 这里我们需要创建两个objective-c文件,最 ...

  8. exif.js解决ios手机上传照片后显示为旋转90度问题(兼容ios13.4之前的版本 )

    exif.js解决ios手机上传照片后显示为旋转90度问题(兼容ios13.4 ) 问题描述: 在做手机移动端app时,发现iOS12.5.1版本(iphone6)上传照片出现顺时针旋转90问题,ip ...

  9. html 里img图片被自动旋转,关于HTML5显示图片翻转90度的问题

    首先,看到标题就觉得这个问题有点看不懂吧.咱遇到问题,那就解决问题.下面就具体描述一下遇到的情况. 上传作品后,需要作品的详情展示,然后在预览页面,出现某些图片与上传的图片展示不一致的效果.下面是原图 ...

最新文章

  1. 使用CSS实现三栏自适应布局(两边宽度固定,中间自适应)
  2. ubuntu kylin 14.04安装配置redis-2.8.9(转)
  3. 家庭中计算机应用包括,《管理信息系统中计算机应用》沟通考笔试B卷答案
  4. .NET Forms身份验证
  5. 类似flashget的浮动窗口的实现
  6. Problem 25
  7. 重磅盘点!2018年更受欢迎的技术干货,来来回回也就看了几十遍吧
  8. Flink学习笔记:搭建Flink on Yarn环境并运行Flink应用
  9. JavaScript Demo - so cool
  10. 字节跳动九周年张一鸣演讲:反对all-in、抽象概念和方法论
  11. mysql为什么用B 树做索引_mysql为什么用b+树做索引
  12. 你对NLP的迁移学习爱的有多深?21个问题弄懂最新的NLP进展。
  13. [转]VS 2005快捷键
  14. java spark命令行执行参数
  15. 如何在酷狗上下载付费歌曲
  16. 史上最详细嵌入式系统设计师修炼手册
  17. 恢复U盘未分配空间怎么合并,u盘分区扩展卷不显示怎么办
  18. uva 167 The Sultan's Successors
  19. 各种设计模式应用场景
  20. 机械革命Code01开启Hyper-V/安装Docker无限蓝屏解决方法

热门文章

  1. 乖宝宠物牵手中泰证券再度备战IPO,宠物食品行业或迎资本战
  2. 基于QT c++开发的音乐播放器
  3. 如何用最短时间找到数据分析工作?
  4. Centos底部面板图标不见的解决办法
  5. 教你用 Python 给自己画一个圣诞帽
  6. javaHTML5互动游戏新闻网站设计与实现计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
  7. SQL语句里||连接符的使用
  8. 500元内什么牌子的游戏蓝牙耳机好用?低延迟游戏蓝牙耳机
  9. 微信开放平台找回过期的authorizer_access_token
  10. HDFS文件压缩与解压缩