Take the 2-minute tour ×

I have a series of sublayers that are created on part of the screen in my app and would like for it to take this and save it as an image, then show the image instead to hopefully make the app run a little faster. How can i get an image saved (temporarily, so not accessible in the photos app) of those layers? They're all inside a UIView if that helps.

share|improve this question
Aren't CALayers cached by default anyway? –  sosborn Mar 28 '11 at 0:29

3 Answers 3

You could what you want with a screenshot :

[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
share|improve this answer

I created this category:


#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>

@interface UIView (ViewCapture)
    - (UIImage*)captureView;


#import "UIView+ViewCapture.h"

@implementation UIView (ViewCapture)

- (UIImage*)captureView
    CGRect rect = self.frame;  
    CGContextRef context = UIGraphicsGetCurrentContext();  
    [self.layer renderInContext:context];  
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();  
    return img;    

Use it like this:

UIImage *screenshot = [aView captureView];


Save and Load UIImage in Documents directory on iPhone

share|improve this answer

Use UIGraphicsBeginImageContext and the related functions, like below (to create an image of a button):

CGContextRef theContext = UIGraphicsGetCurrentContext();
[button.layer renderInContext:theContext];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *theData = UIImagePNGRepresentation(theImage);

For a working example (that will actually save the image in the simulator), see https://github.com/dermdaly/ButtonMaker.

share|improve this answer

Your Answer


