<分区>
在 swift 3.2 应用程序中,我允许用户拍照或从手机图库中选择一张照片。
我在应用程序中显示图片,它的正面朝上。我通过使用 UIImagePNGRepresentation() 方法将 png 转换为数据来将图像保存到目录中。
检索图像时,它是倒置的。 据我所知,我没有在任何地方旋转图像。
<分区>
在 swift 3.2 应用程序中,我允许用户拍照或从手机图库中选择一张照片。
我在应用程序中显示图片,它的正面朝上。我通过使用 UIImagePNGRepresentation() 方法将 png 转换为数据来将图像保存到目录中。
检索图像时,它是倒置的。 据我所知,我没有在任何地方旋转图像。
最佳答案
我也遇到过同样的问题。我想到的唯一解决方法是检查图像的方向是否直立。如果它不是直立的,我们需要从图形上下文中获取正确的图像。
您可以编写一个扩展来修复图像方向,如下所示:(来源:here)
extension UIImage {
func fixOrientation() -> UIImage {
if self.imageOrientation == UIImageOrientation.up {
return self
}
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return normalizedImage;
}
}
然后从相机拍摄照片后调用该方法。
chosenImage = chosenImage.fixOrientation()
关于ios - 在应用程序中保存到手机的 Swift 图片正在旋转 90 度或 180 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46773589/
相关文章:
swift - 更改某些 View Controller 中的状态栏背景颜色 - Swift
swift - 设置自定义 UINavigationBar 的 titleView
iphone - 如何在 ios5 中播放 AVAudioPlayer 本地方法?
database - 在 Swift 中使用变量进行 Firebase 查询
ios - 转换为装箱语法需要转换 UIBarMetrics 警告
ios - 如何从 MWPhoto 对象中获取 UIImage?
ios - 当 tableview 单元格在 xib 上时,如何对 tableview 进行约束。表格 View 在 Storyboard上