Created
April 16, 2013 04:45
-
-
Save luiseduardohd/5393412 to your computer and use it in GitHub Desktop.
Drag&Drop solution, take screenshot of any UIViewController in app with categories and reflection. (subclass must call [super viewDidAppear:animated]; )
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
// | |
// AppDelegate.m | |
// ScreenshotFramework | |
// | |
// Created by Luis Eduardo Hernandez Diaz on 15/04/13. | |
// Copyright (c) 2013 Luis Eduardo Hernandez Diaz. All rights reserved. | |
// | |
#import "AppDelegate.h" | |
//#import "draw2D.h" | |
//#import "CALayer.h" | |
#import <QuartzCore/QuartzCore.h> | |
@interface UIViewController (Screenshot) | |
- (void)viewDidAppearOverride:(BOOL)animated; | |
@end | |
@implementation UIViewController (Screenshot) | |
- (void)viewDidAppearOverride:(BOOL)animated | |
{ | |
NSLog(@"viewDidAppearOverride"); | |
// llamada al metodo base, por que Apple pide que se mande llamar | |
//Como se cambio no se hace recursivo ( espero ) | |
[self viewDidAppearOverride:animated]; | |
UIImageWriteToSavedPhotosAlbum([self screenshot], nil, nil, nil); | |
} | |
//http://developer.apple.com/library/ios/#qa/qa1703/_index.html | |
- (UIImage*)screenshot | |
{ | |
// Create a graphics context with the target size | |
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration | |
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext | |
CGSize imageSize = [[UIScreen mainScreen] bounds].size; | |
if (NULL != UIGraphicsBeginImageContextWithOptions) | |
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); | |
else | |
UIGraphicsBeginImageContext(imageSize); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
// Iterate over every window from back to front | |
for (UIWindow *window in [[UIApplication sharedApplication] windows]) | |
{ | |
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) | |
{ | |
// -renderInContext: renders in the coordinate space of the layer, | |
// so we must first apply the layer's geometry to the graphics context | |
CGContextSaveGState(context); | |
// Center the context around the window's anchor point | |
CGContextTranslateCTM(context, [window center].x, [window center].y); | |
// Apply the window's transform about the anchor point | |
CGContextConcatCTM(context, [window transform]); | |
// Offset by the portion of the bounds left of and above the anchor point | |
CGContextTranslateCTM(context, | |
-[window bounds].size.width * [[window layer] anchorPoint].x, | |
-[window bounds].size.height * [[window layer] anchorPoint].y); | |
// Render the layer hierarchy to the current context | |
[[window layer] renderInContext:context]; | |
// Restore the context | |
CGContextRestoreGState(context); | |
} | |
} | |
// Retrieve the screenshot image | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} | |
+ (void)load | |
{ | |
NSLog(@"load"); | |
method_exchangeImplementations(class_getInstanceMethod(self, @selector(viewDidAppear:)), class_getInstanceMethod(self, @selector(viewDidAppearOverride:))); | |
} | |
@end | |
@implementation AppDelegate | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
// Override point for customization after application launch. | |
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { | |
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; | |
UINavigationController *navigationController = [splitViewController.viewControllers lastObject]; | |
splitViewController.delegate = (id)navigationController.topViewController; | |
} | |
return YES; | |
} | |
- (void)applicationWillResignActive:(UIApplication *)application | |
{ | |
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. | |
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. | |
} | |
- (void)applicationDidEnterBackground:(UIApplication *)application | |
{ | |
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. | |
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. | |
} | |
- (void)applicationWillEnterForeground:(UIApplication *)application | |
{ | |
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. | |
} | |
- (void)applicationDidBecomeActive:(UIApplication *)application | |
{ | |
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. | |
} | |
- (void)applicationWillTerminate:(UIApplication *)application | |
{ | |
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment