Skip to content

Instantly share code, notes, and snippets.

@carlossless
Last active August 29, 2015 13:57
Show Gist options
  • Save carlossless/9579889 to your computer and use it in GitHub Desktop.
Save carlossless/9579889 to your computer and use it in GitHub Desktop.
Create PNG Animation Frame Images by Animating an UIImageView
- (void)makeRotatedImagesWithImageView:(UIImageView *)imageView
{
for (int i = 0; i < 30; i++) {
CGFloat angle = M_PI / 15 * i;
CGSize boundingSize = imageView.image.size;
boundingSize.width *= 2;
boundingSize.height *= 2;
UIGraphicsBeginImageContext(boundingSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, imageView.image.size.width, imageView.image.size.height);
transform = CGAffineTransformRotate(transform, angle);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(context, transform);
// Draw the image into the context
CGContextDrawImage(context, CGRectMake(-imageView.image.size.width, -imageView.image.size.height, imageView.image.size.width * 2, imageView.image.size.height * 2), imageView.image.CGImage);
// Get an image from the context
UIImage *rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];
// Create path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"menu-sync-icon-%d@2x.png", i]];
// Save image.
[UIImagePNGRepresentation(rotatedImage) writeToFile:filePath atomically:YES];
// Clean up
UIGraphicsEndImageContext();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment