views:

1453

answers:

5

I am looking for a way to capture a screenshot on the iPhone with the top status bar included, I am currently using the following code:

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

The above code sucessfully takes a screenshot of the iPhone UIView but does not include the top status bar (In its place is just a blank 20px space).

+2  A: 

You can get the entire contents of the screen by calling the private API UIGetScreenImage. See my previous answer to a similar question for details.

rpetrich
I read about this Private API function before, I am curious to know if Apple will accept the application for the iPhone for using a private API function?
Feelings vary on the use of private APIs. Here's my take:1) Use of private APIs is against Apple's rules and is one of the (maddeningly few) obvious ways your app can get rejected or pulled from the store; but...2) Something as simple as a screenshot is likely to fly under the radar and not be noticed by a reviwer; and yet...3) Apple could change that API at any point in the future, with any release, in any way, for any reason, without any warning. I feel that any use of private APIs is setting yourself up for a support headache and creating the potential for a bad user experience.
zbrimhall
In this particular case, I don't think there's a lot of risk if the function is access dynamically and failure accounted for: the app can just fall back to a screenshot using the standard method. The particular implementation of it in my answer uses static linking though, and that is an exercise for the reader.
rpetrich
A: 

Is UIGetScreenImage still working for you with OS 3.0? if (cgScreen) (from your earlier post) is returning NO for me.

Aral Balkan
+1  A: 

As of Oct 8, 2009, the use of UIGetScreenImage got my app rejected! :( I would not advise using it. I believe Apple is trying to clean up all the apps and make them conform to the new 3.x OS/APIs. I'm looking for an alternative. If anyone has any suggestions. The video API?

jamey
A: 

Instead of using private API, why not render the entire UIWindow into the image context? It might be enough to replace self.view with self.view.window in your code.

You can also get the current window(s) as a property of the [UIApplication sharedApplication] instance. It's possible the status bar is on a separate window layer and maybe you'll have to render the windows in order.

At any rate, you probably don't need to resort to private API.

benzado
A: 

hi i am getting warning for this [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; how to remove it?

zeeshan khan