Last active
December 18, 2015 14:59
-
-
Save clooth/5800945 to your computer and use it in GitHub Desktop.
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
static inline double radians (double degrees) {return degrees * M_PI/180;} | |
CGImageRef CreateRotatedImage(CGImageRef imageRef, UIImageOrientation orientation) | |
{ | |
CGRect bnds = CGRectZero; | |
CGImageRef copy = nil; | |
CGContextRef ctxt = nil; | |
CGRect rect = CGRectZero; | |
CGAffineTransform tran = CGAffineTransformIdentity; | |
bnds.size = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); //self.size; | |
rect.size = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); //self.size; | |
switch (orientation) | |
{ | |
case UIImageOrientationUp: | |
return imageRef; | |
case UIImageOrientationUpMirrored: | |
tran = CGAffineTransformMakeTranslation(rect.size.width, 0.0); | |
tran = CGAffineTransformScale(tran, -1.0, 1.0); | |
break; | |
case UIImageOrientationDown: | |
tran = CGAffineTransformMakeTranslation(rect.size.width, rect.size.height); | |
tran = CGAffineTransformRotate(tran, radians(180.0)); | |
break; | |
case UIImageOrientationDownMirrored: | |
tran = CGAffineTransformMakeTranslation(0.0, rect.size.height); | |
tran = CGAffineTransformScale(tran, 1.0, -1.0); | |
break; | |
default: | |
// orientation value supplied is invalid | |
assert(false); | |
return nil; | |
} | |
UIGraphicsBeginImageContext(bnds.size); | |
ctxt = UIGraphicsGetCurrentContext(); | |
switch (orientation) | |
{ | |
case UIImageOrientationLeft: | |
case UIImageOrientationLeftMirrored: | |
case UIImageOrientationRight: | |
case UIImageOrientationRightMirrored: | |
CGContextScaleCTM(ctxt, -1.0, 1.0); | |
CGContextTranslateCTM(ctxt, -rect.size.height, 0.0); | |
break; | |
default: | |
CGContextScaleCTM(ctxt, 1.0, -1.0); | |
CGContextTranslateCTM(ctxt, 0.0, -rect.size.height); | |
break; | |
} | |
CGContextConcatCTM(ctxt, tran); | |
CGContextDrawImage(ctxt, rect, imageRef); | |
copy = CGBitmapContextCreateImage(ctxt); | |
UIGraphicsEndImageContext(); | |
return copy; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment