Skip to content

Instantly share code, notes, and snippets.

@cypres
Created March 16, 2015 14:06
Show Gist options
  • Save cypres/23b41af9514a50e88967 to your computer and use it in GitHub Desktop.
Save cypres/23b41af9514a50e88967 to your computer and use it in GitHub Desktop.
UIImage from UIImagePickerController resize + fix orientation
import UIKit
extension UIImage {
// Based on http://stackoverflow.com/a/1262395/449607
// Optimized for JPEG output
func imageResized(longEdge : CGFloat) -> UIImage {
func radians (degrees : Double) -> CGFloat {
return CGFloat(degrees * M_PI/180)
}
let ratio = longEdge/max(self.size.height, self.size.width)
let targetHeight = self.size.height * ratio
let targetWidth = self.size.width * ratio
// 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 = CGAffineTransformIdentity
switch (self.imageOrientation) {
case UIImageOrientation.Down, UIImageOrientation.DownMirrored:
transform = CGAffineTransformTranslate(transform, targetWidth, targetHeight)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
break;
case UIImageOrientation.Left,UIImageOrientation.LeftMirrored:
transform = CGAffineTransformTranslate(transform, targetWidth, 0);
transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
break;
case UIImageOrientation.Right, UIImageOrientation.RightMirrored:
transform = CGAffineTransformTranslate(transform, 0, targetHeight);
transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))
break;
case UIImageOrientation.Up, UIImageOrientation.UpMirrored:
break;
}
switch (self.imageOrientation) {
case UIImageOrientation.UpMirrored, UIImageOrientation.DownMirrored:
transform = CGAffineTransformTranslate(transform, targetWidth, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientation.LeftMirrored, UIImageOrientation.RightMirrored:
transform = CGAffineTransformTranslate(transform, targetHeight, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientation.Up, UIImageOrientation.Down, UIImageOrientation.Left, UIImageOrientation.Right:
break;
}
let ctx = CGBitmapContextCreate(
nil, UInt(targetWidth), UInt(targetHeight),
8, UInt(targetWidth)*4,
CGColorSpaceCreateDeviceRGB(),
CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue|CGBitmapInfo.ByteOrder32Little.rawValue)
)
CGContextConcatCTM(ctx, transform)
CGContextSetBlendMode(ctx, kCGBlendModeCopy)
CGContextSetInterpolationQuality(ctx, kCGInterpolationMedium)
switch (self.imageOrientation) {
case UIImageOrientation.Left, UIImageOrientation.LeftMirrored, UIImageOrientation.Right, UIImageOrientation.RightMirrored:
CGContextDrawImage(ctx, CGRectMake(0,0,targetHeight,targetWidth), self.CGImage)
default:
CGContextDrawImage(ctx, CGRectMake(0,0,targetWidth,targetHeight), self.CGImage);
}
let result = CGBitmapContextCreateImage(ctx)
return UIImage(CGImage: result)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment