Created
February 3, 2014 06:25
-
-
Save ryochin/8779607 to your computer and use it in GitHub Desktop.
Rotated UIImage generator
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
- (void) run { | |
UIImage *img = [UIImage imageNamed: @"SampleImage@2x"]; | |
for( int i = 0; i< 40; i++ ){ | |
CGFloat degree = i * 4.5f; | |
NSString *dirName = @"/Users/john/Desktop"; | |
NSString *fileName = [NSString stringWithFormat: @"%@/IconAnim%[email protected]", dirName, i]; | |
NSData *data = [self rotate: img withDegree: degree]; | |
if( [data writeToFile: fileName atomically: YES] == YES ){ | |
NSLog(@"file %@ created.", fileName); | |
} | |
else{ | |
NSLog(@"file %@ failed to create !", fileName); | |
} | |
} | |
} | |
- (NSData *) rotate: (UIImage *) image withDegree: (CGFloat) degree { | |
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0); | |
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextSaveGState(context); | |
// move to center pos | |
CGContextTranslateCTM(context, image.size.width / 2, image.size.height / 2); | |
CGContextScaleCTM(context, 1, -1); | |
CGContextRotateCTM(context, - M_PI_2 / 90 * degree); | |
// move back | |
CGContextTranslateCTM(context, - image.size.width / 2, - image.size.height / 2); | |
CGContextDrawImage(context, frame, image.CGImage); | |
CGContextRestoreGState(context); | |
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return UIImagePNGRepresentation( screenImage ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment