Sibin
Sibin

Reputation: 133

Screen shot of background view in iOS

I want to take screenshot of a particular layer (not the one on top) of my iOS application. I am aware it is possible to do that using 'renderInContect' method of CALayer. But all sample code I found is to take screenshot of current screen (layer on top). If you have any idea on this topic, please share. Thanks

Upvotes: 1

Views: 468

Answers (3)

Paras Joshi
Paras Joshi

Reputation: 20541

Hide other views and controls at that time when you want to capture your view screen shot and after that also visible (Unhide) all controls or views which you hide before capture image

- (UIImage *)captureView {

  //hide controls if needed .. here hide another all view which you not want to display
    CGRect rect = [yourBackView bounds];

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [yourBackView.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //visible controls if needed .. here visible another all view which you want to display
    return img;


}

Upvotes: 1

Rushi
Rushi

Reputation: 4500

UIGraphicsBeginImageContext(self.view.bounds.size); // You can put your view here.
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = [UIImagePNGRepresentation(image) retain];
UIImage *screenShot = [UIImage imageWithData:data];

Upvotes: 1

sergio
sergio

Reputation: 69027

There should be no difference whether your view is in the foreground or even not currently displayed. Use:

UIGraphicsBeginImageContextWithOptions(myBackgroundView.bounds.size, YES, 0.0);
[myBackgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Keep in mind that UIGraphicsBeginImageContextWithOptions is only available from iOS 4.0 on, otherwise use UIGraphicsBeginImageContext.

Upvotes: 5

Related Questions