用相机拍摄出来的照片含有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;
}
复制代码

原文链接

相机获取的照片向左自动旋转90度解决方法相关推荐

  1. iOS开发- 相机(摄像头)获取到的图片自动旋转90度解决办法

    http://www.tuicool.com/articles/IfEZre 今天写demo的时候发现, 如果把通过相机获取到的图片,直接进行操作, 比如裁剪, 缩放, 则会把原图片向又旋转90度. ...

  2. 摄像头camera 旋转90度 解决方法

    拍照需要竖屏时的解决方法. zxing官方wiki上面的解决办法. 基本思路如下. There are 4 relative files: 1.manifest.xml, you need to ma ...

  3. iOS照相机获取到的图片自动旋转90度解决办法

    情景再现: iOS开发过程中,要实现用用手机摄像头拍一张照片,经过裁剪压缩,然后发出去.结果发出去的图片始终是逆时针旋转了90度的图片. 事出有因: 相机拍照后直接取出来的UIimage(用UIIma ...

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

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

  5. 解决 opencv读取手机拍摄的视频可能会自动旋转 90度

    手机或者平板拍摄的视频,  在电脑上opencv 读取, 有的视频会自动旋转90度,因为手机可以横着或者竖着拍摄,网上可以用某些api读取图片的exif信息,在根据信息判断是否做旋转,但是我一直读不出 ...

  6. 解决ios横屏拍照图片自动旋转90度问题

    解决ios横屏拍照图片自动旋转90度问题 参考文章: (1)解决ios横屏拍照图片自动旋转90度问题 (2)https://www.cnblogs.com/lanshengzhong/p/900856 ...

  7. html中如何使图片自动旋转90度,css怎么让图片旋转90度?

    css怎么让图片旋转90度?下面本篇文章给大家介绍一下使用CSS让图片旋转90度的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. css怎么让图片旋转90度? 在CSS中,可以 ...

  8. html中如何使图片自动旋转90度,css实现图片旋转90度的方法

    css实现图片旋转90度的方法 发布时间:2020-08-31 11:44:39 来源:亿速云 阅读:550 作者:小新 小编给大家分享一下css实现图片旋转90度的方法,相信大部分人都还不怎么了解, ...

  9. 图片旋转90度解决办法

    如果把通过相机获取到的图片,直接进行操作, 比如裁剪, 缩放, 则会把原图片向又旋转90度. ps: 查找过程中, 碰到了一种说法: [objc]  view plain copy //get ori ...

最新文章

  1. 慌!年中总结完全没思路,这份安全汇报让你抄作业
  2. 大专学java还是python_零基础应该选择学习 java、php、前端 还是 python?
  3. linux 文本 查看 搜索
  4. 如何将char类型数据转化给int
  5. 作者:金海,博士,华中科技大学计算机科学与技术学院教授、博士生导师。...
  6. 提前批无笔试,3天后截止!!字节跳动智能创作实验室-图像团队2022秋招提前批投递...
  7. [note]抽象类和接口的相同点和不同点
  8. linux命令 选项,Linux常用命令及选项
  9. 面试题之Error和Exception总结
  10. appium的滑动操作总结
  11. [转载]类名.this与类名.class_-Chaz-_新浪博客
  12. html语言制作折线图,html5绘制折线图
  13. 有一个已经排好序的数组,输入一个数,将其插入到数组中,使得数组还是有序的。要求数组元素的值来自初始化
  14. STM32上电启动代码详解(转自安富莱电子)
  15. 简单的网络数据++分类++封装OkHttp
  16. 数据库(Mysql)----mysql查询练习(2、实操)
  17. **Python 复数计算会丢失虚部的问题**ComplexWarning: Casting complex values to real discards the imaginary part
  18. MATLAB筛选数据
  19. cardinality mysql_Cardinality统计取值不准确导致MYSQL选错索引
  20. icem 15.0中IJK control和blank blocks

热门文章

  1. Grafana实现zabbix数据可视化展示
  2. 深圳行:1207-Day 1 - 到达
  3. 有人问我:Linux下命令行里 password:的时候 用键盘密码打不了
  4. Unable to load print control in ReportingService
  5. 让Win让Win XP自动维护系统 自动维护系统
  6. mp3排序软件哪个好用_U盘加密软件_U盘防拷贝软件哪个好用?
  7. 在英特尔® 凌动™ 处理器上将 OpenGL* 游戏移植到 Android* (第二部分)
  8. ERP项目实施记录06
  9. 为Editplus安装smali代码语法高亮插件
  10. Launcher启动的流程图【AndroidICS4.0——Launcher系列四】