Skip to content

Instantly share code, notes, and snippets.

@alexrothenberg
Last active January 2, 2016 21: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 alexrothenberg/8362158 to your computer and use it in GitHub Desktop.
Save alexrothenberg/8362158 to your computer and use it in GitHub Desktop.
-(void)someBigMethod {
// create a screenshot
CALayer * screenLayer = [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] layer];
CGFloat const shotScale = ([[UIScreen mainScreen] scale] / 2.0);
UIGraphicsBeginImageContextWithOptions(screenLayer.bounds.size, YES, shotScale);
[screenLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// put the screenshot in a layer
CALayer * bgLayer = [[CALayer alloc] init];
bgLayer.contentsScale = shotScale;
bgLayer.rasterizationScale = shotScale;
bgLayer.opaque=YES;
bgLayer.bounds = CGRectMake(0, 0, screenshot.size.width, screenshot.size.height);
bgLayer.contents = (id) screenshot.CGImage;
// lots more lines to do more stuff
}
-(void)someBigMethod {
UIImage* screenshot = createScreenshot();
CALayer * bgLayer = putScreenshotInALayer(screenshot);
// lots more 1 line function calls
}
// with a bunch of private helper methods that each do one thing like
-(UIImage*)createScreenshot {
CALayer * screenLayer = [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] layer];
CGFloat const shotScale = ([[UIScreen mainScreen] scale] / 2.0);
UIGraphicsBeginImageContextWithOptions(screenLayer.bounds.size, YES, shotScale);
[screenLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenshot;
}
-(CALayer*)putScreenshotInALayer(UIImage *)screenshot {
CALayer * bgLayer = [[CALayer alloc] init];
bgLayer.contentsScale = shotScale;
bgLayer.rasterizationScale = shotScale;
bgLayer.opaque=YES;
bgLayer.bounds = CGRectMake(0, 0, screenshot.size.width, screenshot.size.height);
bgLayer.contents = (id) screenshot.CGImage;
return bgLayer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment