Skip to content

Instantly share code, notes, and snippets.

@viccc
Created November 11, 2015 01:42
Show Gist options
  • Save viccc/07efa80fc8ddc843ea71 to your computer and use it in GitHub Desktop.
Save viccc/07efa80fc8ddc843ea71 to your computer and use it in GitHub Desktop.
Re-render UIImage with .Up orientation if it has different orientation. Swift.
func normalizeImageOrientationIfNeeded(image: UIImage) -> UIImage {
if image.imageOrientation == .Up {
return image
}
let size = image.size
let scale = image.scale
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, scale)
image.drawInRect(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment