Skip to content
This repository was archived by the owner on Jul 1, 2022. It is now read-only.

Commit 7aaefd4

Browse files
Fix orientation of resized image - Jason & Wes
1 parent 01d223a commit 7aaefd4

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

ALCameraViewController/ViewController/ConfirmViewController.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,22 @@ extension UIImage {
346346
rectTransform = rectTransform.scaledBy(x: scale, y: scale)
347347

348348
if let cropped = cgImage?.cropping(to: rect.applying(rectTransform)) {
349-
return UIImage(cgImage: cropped)
349+
return UIImage(cgImage: cropped, scale: scale, orientation: imageOrientation).fixOrientation()
350350
}
351351

352352
return self
353353
}
354+
355+
func fixOrientation() -> UIImage {
356+
if self.imageOrientation == UIImageOrientation.up {
357+
return self
358+
}
359+
360+
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
361+
self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
362+
let normalizedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() ?? self
363+
UIGraphicsEndImageContext()
364+
365+
return normalizedImage
366+
}
354367
}

0 commit comments

Comments
 (0)