Skip to content

Instantly share code, notes, and snippets.

@DaveBatton
Last active January 4, 2016 01: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 DaveBatton/8547787 to your computer and use it in GitHub Desktop.
Save DaveBatton/8547787 to your computer and use it in GitHub Desktop.
Saves any images found in the specified view or any of its subviews. Images are saved in the application's Documents directory.
- (void)stealImagesFromView:(UIView *)view
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UIImageView class]]) {
UIImageView *imageView = (UIImageView *)subview;
if (imageView.image) {
NSString *filename = [NSString stringWithFormat:@"image (%f)", [NSDate timeIntervalSinceReferenceDate]];
filename = [filename stringByAppendingPathExtension:@"png"];
NSString *path = [documentsDirectory stringByAppendingPathComponent:filename];
NSData *imageData = UIImagePNGRepresentation(imageView.image);
[imageData writeToFile:path atomically:YES];
}
}
[self stealImagesFromView:subview];
}
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"Stolen images can be found here: %@", documentsDirectory);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment