从文件中检索图像时旋转

Image gets rotated when retrieved from file

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

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

第一个VC在拍照并点击UIPickerViewController界面上的"Use photo"按钮后是这样的:

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

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

谢谢!感谢所有帮助!

我得到了我的答案 right here,这与 @ocanal 在他上面的评论中提供的 link 只有一两次点击。我确实必须从 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)