Last active
February 14, 2021 23:25
-
-
Save wavecos/8718bd15064a1b688f92 to your computer and use it in GitHub Desktop.
Fix Image Orientation, Rotation #swift #ios
This file contains hidden or 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
http://stackoverflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload | |
extension UIImage { | |
func fixOrientation() -> UIImage { | |
if (self.imageOrientation == UIImageOrientation.Up) { | |
return self; | |
} | |
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale); | |
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) | |
self.drawInRect(rect) | |
var normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext(); | |
return normalizedImage; | |
} | |
} | |
func normalizedImage() -> UIImage { | |
if (self.imageOrientation == UIImageOrientation.Up) { | |
return self; | |
} | |
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale); | |
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) | |
img.drawInRect(rect) | |
var normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext(); | |
return normalizedImage; | |
} | |
func fixOrientation(img:UIImage) -> UIImage { | |
if (img.imageOrientation == UIImageOrientation.Up) { | |
return img; | |
} | |
UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale); | |
let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height) | |
img.drawInRect(rect) | |
var normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext(); | |
return normalizedImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment