56wcfq
Last Updated: October 11, 2018
·
21.87K
· 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

4 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 ·
30939

Is there a way to disable the image preview/editor you get after taking a screenshot on the latest iOS 11 version, it bugs me because I have no use for it personally and whenever I want to use my phone normally after taking a screenshot I need to manually swipe the little icon to get rid of it.

https://appsync.biz/abcya/ https://appsync.biz/adam4adam/ https://appsync.biz/pof/

29 days ago ·