我目前正在 Swift 中从文档文件夹中保存和加载图像。图像保存并加载正常,但一些图像在加载到图像视图时会旋转。
这是我的保存密码
let url = documents.appendingPathComponent("image.png")
if let data = imgKort.image?.pngData() {
do {
try data.write(to: url)
print("Image Saved")
} catch {
print("Unable to Write Image Data to Disk")
}
}
这是我将图像加载回图像视图的代码
if let imageURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("image.png") {
if let image = UIImage(contentsOfFile: imageURL.path) {
let rotatedImage = UIImage(cgImage: image.cgImage!, scale: image.scale, orientation: image.imageOrientation)
imgKort.image = rotatedImage
}
}