ios - 裁剪 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()
}
我不知道如何将它们保存在我想要的方向。请帮忙!
解决方案
从下面的行更改
let croppedUIImage = UIImage(cgImage: croppedCGImage)
至
let croppedUIImage = UIImage(cgImage: croppedCGImage, scale: imageToSave.scale, orientation: imageToSave.imageOrientation)
推荐阅读
- python - 以非常奇怪的方式读取文本文件
- c++ - 用于将 std::osyncstream 和 std::cout 组合在一起的 C++20 自定义类
- c++ - 在 map 中,键值对不存储在传染性内存中,因此迭代器如何使用它从一个键值对跳转到另一个键值对++
- python - Pygame俄罗斯方块块在某些条件下不向下移动
- swiftui - SwiftUI:当前视图从点击视图中增长并关闭缩回视图?
- javascript - 成功后如何使我的贝宝付款重定向?
- reactjs - 用 axios 测试 React 登录组件
- mysql - 获取员工成绩最终分数查询
- php - 我们可以在 Laravel 迁移中运行查询吗?
- python - Python Token请求不同大小