Saturday, February 07, 2009

Programmatically Taking a Screenshot in iPhone

It took me some time to find the code for this. Thought I'd share it here. The following code takes a screen capture of the currently visible portions of a UITableView but its applicable to any view.

UIGraphicsBeginImageContext(self.tableView.bounds.size);
[self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
//UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
UIGraphicsEndImageContext();

The commented out line saves the photo to a the Photos application on the iPhone.

9 comments:

Mary said...

I notice you say that this code should work for any view...however, when I try it on a UIWebView that is not visible, the image is just a gray square. If I add the UIWebView to the currently displayed view , it works fine. If I add the UIWebView to a view that is NOT currently displayed, it is just a gray square. Any thoughts?

Aashish Patil said...

Hi Mary,

Apologize for the confusion. I was referring to all subclasses of UIView.

The code is meant to take a screenshot of a view that is visible on the screen. I don't believe it can work for a non-visible object since its not really on the screen.

Thanks,
Aashish

blogger@paulhoepner.de said...

Any Idea how to do this with the MPMoviePlayer? I Only get a black UIImage when trying to capture the MPVideoView

Aashish Patil said...

Hi Paul,

I've not tried this with MPMoviePlayer. Maybe try using UIWebView and have that open the movie.

I've taken a screenshot of the built in video player using the standard method of pressing the 'Home' and 'Power' button simultaneously and that had worked. So, I know there is a way but not sure of the issues related to doing this programmatically.

Thanks,
Aashish

Walter Tyree said...

Something I noticed was that my XCode didn't reference Quartz so I had to include a reference to it to get rid of some errors.

#include <QuartzCore/QuartzCore.h>

Jorsh said...

Thanks! Definitely solved my problem :)

まえやま said...

> Mary, Asashish

You can take screenshots of invisible views as well.
You just need to create an offscreen UIWindow instance and add your view to it.

UIWebView *webView = [[UIWebView alloc] initWithFrame:theFrame];
UIWindow *offscreenWindow = [[UIWindow alloc] initWithFrame:theFrame];
[offscreenWindow addSubview:webView];
// Now take the screenshot

It seems that UIWebView doesn't get rendered unless it's a subview of a window.

Unknown said...

Hi Aashish, i am creating an app that has an image with a hole cut out and the camera shows in the background. When i do the manual screenshot(home+power) it shows the image with the persons face(camera) in the hole. when i create a button to do this it doesnt work, the hole where the face is, is simply white. any help would be appreciated.

Aashish Patil said...

Hi Trent,

The above method may not work with Open GL and Video views. You might want to check out the Apple developer forums in the Open GL section for code on how to this.

Aashish

  DB Reading BASE: An Acid Alternative: In partitioned databases, trading some consistency for availability can lead to dramatic improvement...