Skip to content

Instantly share code, notes, and snippets.

@btomtom5
Created May 25, 2018 17:39
Show Gist options
  • Select an option

  • Save btomtom5/13b6224f62d7c9b641906491d2eb8408 to your computer and use it in GitHub Desktop.

Select an option

Save btomtom5/13b6224f62d7c9b641906491d2eb8408 to your computer and use it in GitHub Desktop.
Properly rotate the underlying cgImage of a UIimage given its orientation
func fixOrientation(uiImage: UIImage) -> UIImage{
// No-op if the orientation is already correct
if ( uiImage.imageOrientation == UIImageOrientation.up ) {
return UIImage(cgImage: uiImage.cgImage!, scale: uiImage.scale, orientation: uiImage.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 ( uiImage.imageOrientation == UIImageOrientation.down || uiImage.imageOrientation == UIImageOrientation.downMirrored ) {
transform = transform.translatedBy(x: uiImage.size.width, y: uiImage.size.height)
transform = transform.rotated(by: CGFloat(Double.pi))
}
if ( uiImage.imageOrientation == UIImageOrientation.left || uiImage.imageOrientation == UIImageOrientation.leftMirrored ) {
transform = transform.translatedBy(x: uiImage.size.width, y: 0)
transform = transform.rotated(by: CGFloat(Double.pi/2))
}
if ( uiImage.imageOrientation == UIImageOrientation.right || uiImage.imageOrientation == UIImageOrientation.rightMirrored ) {
transform = transform.translatedBy(x: 0, y: uiImage.size.height)
transform = transform.rotated(by: CGFloat(-Double.pi/2)) }
if ( uiImage.imageOrientation == UIImageOrientation.upMirrored || uiImage.imageOrientation == UIImageOrientation.downMirrored ) {
transform = transform.translatedBy(x: uiImage.size.width, y: 0)
transform = transform.scaledBy(x: -1 , y: 1)
}
if ( uiImage.imageOrientation == UIImageOrientation.leftMirrored || uiImage.imageOrientation == UIImageOrientation.rightMirrored ) {
transform = transform.translatedBy(x: uiImage.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.
let ctx: CGContext = CGContext(data: nil, width: Int(uiImage.size.width), height: Int(uiImage.size.height),
bitsPerComponent: (uiImage.cgImage?.bitsPerComponent)!, bytesPerRow: 0,
space: (uiImage.cgImage!.colorSpace)!,
bitmapInfo: uiImage.cgImage!.bitmapInfo.rawValue)!
ctx.concatenate(transform)
if ( uiImage.imageOrientation == UIImageOrientation.left ||
uiImage.imageOrientation == UIImageOrientation.leftMirrored ||
uiImage.imageOrientation == UIImageOrientation.right ||
uiImage.imageOrientation == UIImageOrientation.rightMirrored ) {
ctx.draw(uiImage.cgImage!, in: CGRect(x: 0,y: 0,width: uiImage.size.height,height: uiImage.size.width))
} else {
ctx.draw(uiImage.cgImage!, in: CGRect(x: 0,y: 0,width: uiImage.size.width,height: uiImage.size.height))
}
// And now we just create a new UIImage from the drawing context and return it
return UIImage(cgImage: ctx.makeImage()!)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment