Skip to content

Instantly share code, notes, and snippets.

@liamnichols
Created September 2, 2013 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liamnichols/6413368 to your computer and use it in GitHub Desktop.
Save liamnichols/6413368 to your computer and use it in GitHub Desktop.
Renders all the sublayers of all the subviews within a specified view to file
static NSString *dir = @"/Users/liam.nichols/Desktop/layers/";
-(void)sublayersToFile
{
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
if ([window isKindOfClass:NSClassFromString(@"UITextEffectsWindow")])
{
[self subviewsInView:window];
break;
}
}
}
-(void)subviewsInView:(UIView *)superview
{
for (UIView *view in superview.subviews)
{
[self getLayers:view];
[self subviewsInView:view];
}
}
-(void)getLayers:(UIView *)view
{
NSArray *sublayers = view.layer.sublayers;
for (CALayer *layer in sublayers)
{
@autoreleasepool
{
NSLog(@"Working on layer: %p",layer);
UIImage *image = [self imageFromLayer:layer];
NSString *filename = [dir stringByAppendingPathComponent:[NSString stringWithFormat:@"%p.png",layer]];
[UIImagePNGRepresentation(image) writeToFile:filename atomically:YES];
}
}
}
- (UIImage *)imageFromLayer:(CALayer *)layer
{
UIGraphicsBeginImageContextWithOptions([layer frame].size, NO, 0);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment