56wcfq
Last Updated: January 28, 2019
·
23.34K
· mystcolor

Fast snapshot any views on iOS 7

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];

Download from source

3 Responses
Add your response

10665

This is great, definitely will be using this. Thanks for the tip on the faster API in iOS 7!

over 1 year ago ·
10675

Glad you like it :)

over 1 year ago ·
14109

HyperImageView is a an alternative for UIImageView which renders images 30 times faster than UIImageView while uses 10 times less RAM. Also the rendering task is processed in background and it never blocks user interaction.

http://hyperimageview.com

over 1 year ago ·