ios - 从文件中检索时图像会旋转

标签 ios swift uiimage

我正在修改 an online lesson on using UIPickerViewController 中的一些代码.

我正在拍摄一张照片,显示在我的第一个 View Controller 的 UIImageView 中,然后将其保存到文件中。然后我检索它并将其显示在第二个 View Controller 的 UIImageView 中。 UIImageView 的大小相同 (375 x 315),并使用相同的设置:

enter image description here

拍摄图片并点击 UIPickerViewController 界面上的“使用照片”按钮后,第一个 VC 看起来像这样:

enter image description here

在我检索图片后,第二个 VC 看起来像这样(我使用的是 PNG):

enter image description here

我在 SO 上折腾了很多,但还没有找到 iOS 的解决方案。有人可以指出我正确的方向吗?我可以提供任何代码(看起来不起眼),但我不认为这就是问题所在。

谢谢!感谢所有帮助!

最佳答案

我得到了答案 right here ,距离 @ocanal 在上面的评论中提供的链接只有一两次点击。我确实必须从 Obj C 转换为 Swift,但它就像魔术一样工作。这是我为实现正确方向而添加的代码:

让originalImage = UIImage(contentsOfFile: imagePath)

var imageToDisplay: UIImage? = nil
if let anImage = originalImage?.cgImage {
    imageToDisplay = UIImage(cgImage: anImage, scale: originalImage!.scale, orientation: .right)

关于ios - 从文件中检索时图像会旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51370284/

相关文章:

ios - 检测带有圆角的卡片边缘

objective-c - ASIHTTPRequestTester : Asynchronous not working

ios - 类中的类型别名与 swift 中函数中的类型别名 (iOS)

ios - 如何正确加载依赖的 tableViewCell API 数据?

ios - 将父属性重新定义为 swift 中的子属性

iphone - 为iPhone和iPad加载不同的图像

ios - ScrollView 中的内容随着导航菜单的高度值向下移动

ios - 从 JSON 填充表

ios - 如何在 iOS 中获取 Firebase URL?

ios - fatal error : Unexpectedly found nil while unwrapping an Optional value NSURL