首页 > 解决方案 > 裁剪 UIImage 使其在保存时逆时针旋转 90º

问题描述

我正在制作一个带有全屏摄像头的应用程序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()
    }

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

标签: iosuiimageswift4cgimage

解决方案


从下面的行更改

let croppedUIImage = UIImage(cgImage: croppedCGImage)

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

推荐阅读