裁剪 UIImage 使其在保存时逆时针旋转 90º

Cropping a UIImage makes it rotate 90º anti-clockwise upon saving

我正在开发一个应用程序,该应用程序具有带 AVFoundation 的全屏摄像头。我是直接保存 UIImage 而不压缩它。它给了我想要的输出,即它正在保存图像的拍摄方向。但是,当我尝试将其裁剪为矩形 (CGRect) 时,它会像以前一样保存图像。这是我所做的:

@objc func saveTapped() {
        myIndicator.isHidden = false
        guard let imageToSave = imageToShow else { return }
        let crop = CGRect(x: 0, y: 0, width: 1440.0, height: 2560.0)
        let croppedCGImage = imageToSave.cgImage?.cropping(to: crop)
        let croppedUIImage = UIImage(cgImage: croppedCGImage!)
        UIImageWriteToSavedPhotosAlbum(croppedUIImage, nil, nil, nil)
        myIndicator.stopAnimating()
    }

我不知道如何将它们保存在我想要的方向。请帮忙!

更改下面的行
let croppedUIImage = UIImage(cgImage: croppedCGImage)

let croppedUIImage = UIImage(cgImage: croppedCGImage, scale: imageToSave.scale, orientation: imageToSave.imageOrientation)