Created
September 2, 2013 14:19
-
-
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
This file contains 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 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