commit 196cfdec4ba17009626cc2a690a8741148c56c91
parent bfb47c0f855772f739b146a7f8232a9cbb9d2c4d
Author: Joel Klabo <joelklabo@gmail.com>
Date: Tue, 4 Apr 2023 17:59:54 -0700
Fix Image Orientation
Diffstat:
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/damus/Views/ImagePicker.swift b/damus/Views/ImagePicker.swift
@@ -45,11 +45,13 @@ struct ImagePicker: UIViewControllerRepresentable {
// Handle the selected image
onImagePicked(imageURL)
} else if let cameraImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
- if let imageURL = saveImageToTemporaryFolder(image: cameraImage, imageType: "jpeg") {
+ let orientedImage = cameraImage.fixOrientation()
+ if let imageURL = saveImageToTemporaryFolder(image: orientedImage, imageType: "jpeg") {
onImagePicked(imageURL)
}
} else if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
- if let editedImageURL = saveImageToTemporaryFolder(image: editedImage) {
+ let orientedImage = editedImage.fixOrientation()
+ if let editedImageURL = saveImageToTemporaryFolder(image: orientedImage, imageType: "jpeg") {
onImagePicked(editedImageURL)
}
}
@@ -118,3 +120,16 @@ struct ImagePicker: UIViewControllerRepresentable {
}
}
+
+extension UIImage {
+ func fixOrientation() -> UIImage {
+ guard imageOrientation != .up else { return self }
+
+ UIGraphicsBeginImageContextWithOptions(size, false, scale)
+ draw(in: CGRect(origin: .zero, size: size))
+ let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()
+ UIGraphicsEndImageContext()
+
+ return normalizedImage ?? self
+ }
+}