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

时间:2018-06-08 12:06:29

标签: ios uiimage swift4 cgimage

我正在制作一款带有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()
    }

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

1 个答案:

答案 0 :(得分:0)

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

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