Skip to content

Instantly share code, notes, and snippets.

@ivanovvitaly
Created July 6, 2015 20:21
Show Gist options
  • Save ivanovvitaly/dc7d8c90195bbeab3e32 to your computer and use it in GitHub Desktop.
Save ivanovvitaly/dc7d8c90195bbeab3e32 to your computer and use it in GitHub Desktop.
UIImage category written in Swift that allows to fix the Image orientation and resize Image by long edge before uploading to the server
import Foundation
extension UIImage {
func fixOrientation() -> UIImage {
let width = CGImageGetWidth(self.CGImage)
let height = CGImageGetHeight(self.CGImage)
var transform = CGAffineTransformIdentity
var bounds = CGRect(x:0, y:0, width:width, height:height)
let scaleRatio = bounds.size.width / CGFloat(width)
let imageSize = CGSize(width: width, height: height)
var boundHeight: CGFloat
let orient = self.imageOrientation
switch(orient) {
case UIImageOrientation.Up: //EXIF = 1
transform = CGAffineTransformIdentity
case UIImageOrientation.UpMirrored: //EXIF = 2
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0)
transform = CGAffineTransformScale(transform, -1.0, 1.0)
case UIImageOrientation.Down: //EXIF = 3
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
case UIImageOrientation.DownMirrored: //EXIF = 4
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height)
transform = CGAffineTransformScale(transform, 1.0, -1.0)
case UIImageOrientation.LeftMirrored: //EXIF = 5
boundHeight = bounds.size.height
bounds.size.height = bounds.size.width
bounds.size.width = boundHeight
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width)
transform = CGAffineTransformScale(transform, -1.0, 1.0)
transform = CGAffineTransformRotate(transform, 3.0 * CGFloat(M_PI) / 2.0)
case UIImageOrientation.Left: //EXIF = 6
boundHeight = bounds.size.height
bounds.size.height = bounds.size.width
bounds.size.width = boundHeight
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width)
transform = CGAffineTransformRotate(transform, 3.0 * CGFloat(M_PI) / 2.0)
case UIImageOrientation.RightMirrored: //EXIF = 7
boundHeight = bounds.size.height
bounds.size.height = bounds.size.width
bounds.size.width = boundHeight
transform = CGAffineTransformMakeScale(-1.0, 1.0)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI) / 2.0)
case UIImageOrientation.Right: //EXIF = 8
boundHeight = bounds.size.height
bounds.size.height = bounds.size.width
bounds.size.width = boundHeight
transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI) / 2.0)
default:
fatalError("Invalid image orientation")
}
UIGraphicsBeginImageContext(bounds.size);
var context = UIGraphicsGetCurrentContext();
if (orient == UIImageOrientation.Right || orient == UIImageOrientation.Left) {
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
CGContextTranslateCTM(context, CGFloat(-height), 0.0);
} else {
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextTranslateCTM(context, 0.0, CGFloat(-height))
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRect(x:0, y:0, width:width, height:height), self.CGImage)
var imageCopy = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return imageCopy
}
func resizeByLongEdge(length: CGFloat) -> UIImage {
let width = CGFloat(CGImageGetWidth(self.CGImage))
let height = CGFloat(CGImageGetHeight(self.CGImage))
var bounds = CGRect(x:0.0, y:0.0, width:width, height:height)
if (width > length || height > length) {
var ratio = width / height
if ratio > 1 {
bounds.size.width = CGFloat(length)
bounds.size.height = bounds.size.width / ratio
}
else {
bounds.size.height = CGFloat(length)
bounds.size.width = bounds.size.height * ratio
}
UIGraphicsBeginImageContext(bounds.size)
var context = UIGraphicsGetCurrentContext()
self.drawInRect(bounds)
var scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage
}
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment