Skip to content

Instantly share code, notes, and snippets.

@ryochin
Created February 3, 2014 06:25
Show Gist options
  • Save ryochin/8779607 to your computer and use it in GitHub Desktop.
Save ryochin/8779607 to your computer and use it in GitHub Desktop.
Rotated UIImage generator
- (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