-
-
Save unixb0y/1efbf6f14f43a9a0895bd77fce84ad3c to your computer and use it in GitHub Desktop.
UIImage fix orientation ( rotate pixel rather than relying on exif ) -- Swift 4, no force unwraps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIImage { | |
func fixOrientation() -> UIImage { | |
guard let cg = cgImage, let cg_colorspace = cg.colorSpace else { | |
print("Error while fixing image orientation") | |
return self | |
} | |
// No-op if the orientation is already correct | |
if self.imageOrientation == .up { | |
return UIImage(cgImage: cg, scale: scale, orientation: imageOrientation) | |
} | |
// We need to calculate the proper transformation to make the image upright. | |
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. | |
var transform: CGAffineTransform = CGAffineTransform.identity | |
if imageOrientation == .down || imageOrientation == .downMirrored { | |
transform = transform.translatedBy(x: self.size.width, y: self.size.height) | |
transform = transform.rotated(by: CGFloat(Double.pi)) | |
} | |
if imageOrientation == .left || imageOrientation == .leftMirrored { | |
transform = transform.translatedBy(x: self.size.width, y: 0) | |
transform = transform.rotated(by: CGFloat(Double.pi / 2)) | |
} | |
if imageOrientation == .right || imageOrientation == .rightMirrored { | |
transform = transform.translatedBy(x: 0, y: self.size.height) | |
transform = transform.rotated(by: CGFloat(-Double.pi / 2)) | |
} | |
if imageOrientation == .upMirrored || imageOrientation == .downMirrored { | |
transform = transform.translatedBy(x: self.size.width, y: 0) | |
transform = transform.scaledBy(x: -1, y: 1) | |
} | |
if imageOrientation == .leftMirrored || imageOrientation == .rightMirrored { | |
transform = transform.translatedBy(x: self.size.height, y: 0) | |
transform = transform.scaledBy(x: -1, y: 1) | |
} | |
// Now we draw the underlying CGImage into a new context, applying the transform | |
// calculated above. | |
guard let ctx = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height), bitsPerComponent: cg.bitsPerComponent, bytesPerRow: 0, space: cg_colorspace, bitmapInfo: cg.bitmapInfo.rawValue) else { | |
print("Error while fixing image orientation") | |
return self | |
} | |
ctx.concatenate(transform) | |
if imageOrientation == .left || | |
imageOrientation == .leftMirrored || | |
imageOrientation == .right || | |
imageOrientation == .rightMirrored { | |
ctx.draw(cg, in: CGRect(x: 0, y: 0, width: self.size.height, height: self.size.width)) | |
} | |
else { | |
ctx.draw(cg, in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)) | |
} | |
// And now we just create a new UIImage from the drawing context and return it | |
guard let res = ctx.makeImage() else { | |
print("Error while fixing image orientation") | |
return self | |
} | |
return UIImage(cgImage: res) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment