Ted Ted - 1 year ago 68
iOS Question

make a UIView with added subviews into a single image?

I have a view with added subviews. I would like to turn that view with many subviews into a single image or view.

How is that possible?


Answer Source

On iOS7 you can use the new [UIView snapshotViewAfterScreenUpdates:] method.

To support older OS you can render any view into an UIImage with Core Graphics. I use this category on UIView for snapshots:


#import <UIKit/UIKit.h>

@interface UIView (Snapshot)
- (UIImage *)snapshotImage;


#import "UIView+Snapshot.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIView (Snapshot)

- (UIImage *)snapshotImage
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    return resultingImage;


It requires QuartzCore framework, so make sure to add that to your project.

To use it import the header and:

UIImage *snapshot = [interestingView snapshotImage];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download