从视频生成缩略图最终总是在 UIImageView 中旋转

Generating thumbnail from Video ends up always rotated in an UIImageView

我正在使用 UIImagePickerController 来 select 用户图库中的视频或图片。有了照片,就没问题了,一切都按原样显示。当我尝试为视频生成缩略图时出现问题。

基本上,缩略图最终可能会上下颠倒或向左或向右倾斜 90 度显示。我不确定为什么会这样。我是漏了什么还是做错了什么?

extension PostVC : UIImagePickerControllerDelegate, UINavigationControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
        if let thumbnail = self.generateThumbnailForImage(videoUrl) {
            self.videoUrl = videoUrl
            self.photoImageView.image = thumbnail
            self.selectedImage = thumbnail
        }
    }
    if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        selectedImage = image
        photoImageView.image = image
        shareButton.isEnabled = true
    }
    dismiss(animated: true, completion: nil)
}

func generateThumbnailForImage(_ fileUrl: URL) -> UIImage? {
    let asset = AVAsset(url: fileUrl)
    let imageGenerator = AVAssetImageGenerator(asset: asset)
    do {
        let thumbnailCGImage = try imageGenerator.copyCGImage(at: CMTimeMake(value: 1, timescale: 10), actualTime: nil) // 1 sec
        return UIImage(cgImage: thumbnailCGImage)

    } catch let err {
        SVProgressHUD.showError(withStatus: err.localizedDescription)
    }
    return nil
  }
}

设置 imageGenerator.appliesPreferredTrackTransform = true 似乎可以解决这个问题。