Created
June 29, 2016 10:49
-
-
Save acalism/9b7b94c04fd73c503396b040ab29967f to your computer and use it in GitHub Desktop.
Snapshot any view for iOS
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
// Might be you will need to snapshot a particular view into an image. Previously we do like this: | |
// #import <QuartzCore/QuartzCore.h> | |
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0); | |
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; | |
UIImage *copied = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
//On iOS 7 there's an updated API that is much faster: | |
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0); | |
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]; | |
UIImage *copied = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
//How efficient is the faster API? It is about 1.8x faster on iPhone 5. Time elapsed was down from 90ms to 50ms. | |
//If you want to create a view already drawn with the image, we can use an even more handy API: | |
UIView *newView = [view snapshotViewAfterScreenUpdates:YES]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment