56wcfq
Last Updated: September 16, 2016
·
19.21K
· mystcolor
Avatar 400

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
C3b9a15ccd23da3d3479ee92bf6a8578

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

over 1 year ago ·
10675
Avatar 400

Glad you like it :)

over 1 year ago ·
14109
97a2875855fd7d407270c0041d7840db

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 ·