iPhone 相机图片旋转后出现——CGAffineTransformInvert:奇异矩阵
我从图像选择器获取 UIImage 并将其放入 UIImageView 中。 它对于常规图像效果很好,但对于使用 iPhone 相机拍摄的图像,它们有时会旋转 90 度。
当稍后访问其中一张图像时(它已经保存到磁盘),我注意到上述错误。
有任何想法吗? 我认为这与 iPhone 在拍摄照片时存储有关相机位置的额外 exif 元数据有关,但是其中一张照片是在纵向模式下拍摄的,不知何故旋转为横向。
I'm getting the UIImage from the image picker and putting it into a UIImageView. It works fine for regular images, but for images taken with the iphone camera, they sometimes come up rotated 90 degrees.
When accessing one of those images later (it has already been saved to disk) I noticed the above error.
Any ideas? I assume it has something to do with the iphone storing extra exif metadata about the camera position while the photo was taken, however one of the pictures was taken in portrait mode and somehow got rotated to landscape.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用的解决方案是旋转 & 按照
imageOrientation
属性的指示翻转图像。 这里有一些代码可以解决这个问题: UIImage fix 。UIImageView 旋转 & 自动翻转,所以如果你所做的只是将其显示在屏幕上,那么这不是问题。
The solution I've used is to rotate & flip the image as dictated by the
imageOrientation
property. There is some code here that will do the trick: UIImage fix.A UIImageView rotates & flips automatically, so it's not a problem if all you do is display it on screen like that.
你是对的,手机存储旋转元数据和图像来指示它应该以哪种方式旋转以显示。 这可能是 EXIF 标签; 它可能是完全不同的东西,苹果告诉你的一切都是 UIImage 的形式。
UIImage 在很多方面都是 CGImage 的简单包装。 在这种情况下,CGImage 图像是一个“哑”对象,只知道/关心原始图像数据; 没有像旋转信息这样高级的东西。 UIImage 将其与旋转和其他信息结合在一个方便的类中。 好处之一是 UIImage 自动知道如何以正确的旋转来绘制自身。
因此,黄金基本上可以是在大多数应用程序中避免与 CGImage 交互。 UIImage 应该做几乎所有你想做的事情,并处理更高级别的旋转信息等。 否则,您会遇到奇怪的情况,大多数图像绘制正确,但有些图像旋转。
You are right that the phone stores rotation metadata with an image to indicate which way it should be rotated for display. This could be an EXIF tag; it could be something completely different, all Apple tells you is in the form of UIImage.
UIImage is in many respects, a simple wrapper around CGImage. CGImage image is in this case a "dumb" object that only knows/cares about raw image data; not anything so high-level as rotation info. UIImage brings this together with rotation and other info im a single convenient class. One of the perks is that UIImage automatically knows how to draw itself with the right rotation.
So the golden can basically be to avoid interacting with CGImage in most apps. UIImage should do almost everything you want, and handle higher level rotation info etc. too. Otherwise you get into odd situations where most images draw correctly, but some are rotated.