iPhone – Capture Screenshot Programmatically

Capturing screenshot on iPhone needs the QuartzCore framework.

1. Right click the Framework folder in your Xcode project.

2. Add -< Add Existing Frameworks

3. Choose the QuartzCore.framework

 

4. Import the QuartzCore framework in the .h header file

#import <QuartzCore/QuartzCore.h>

 

5. Add the following few lines in the .m implementation file to capture the screenshot

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

 

6. Try it

Done =)

Reference: Possible to make a screenshot programmatically?

12 thoughts on “iPhone – Capture Screenshot Programmatically”

  1. I used this code to take the snapshot of my app running on simulator, the quality of the snap is not good, Any idea?

    Like

  2. hi,thanks for the help
    my problem is is want to take the screenshot in landscape mode itself but this code give me a portrait mode screenshot.which make my screenshot hard to handle even after rotating it
    CurrentScreen.transform = CGAffineTransformMakeRotation(4.71238898);
    thats how i rotate my image

    Like

  3. I’m at Startup Weekend in LA right now where a few of the iPhone developers here have said that it’s impossible to take a screenshot programmatically between applications due to iOS sandbox restrictions but I’m not convinced of this. Two tutorials explain how to capture a screenshot programmatically when using a single application (see “Programmatically Getting a Screencapture of any UIView”, http://www.icodeblog.com/2009/07/27/1188/ and “iPhone – Capture Screenshot Programmatically”, https://ykyuen.wordpress.com/2010/04/10/iphone-capture-screenshot-programmatically/) but I am trying to figure out if a screenshot can be taken between one application and another like when a new text message comes in when the iPhone is not locked, and when it is.

    Any help will be appreciated…thanks.

    Noah

    Like

  4. Hi Noah,

    Do you mean you want to take a screenshot and save it to the iphone album when
    1. The iPhone is unlocked
    2. The iPhone is locked
    3. When a sms is received

    if that is the case, i think you can refer to the following Q&A for the 1st and 2nd scenarios.
    StackOverflow – Lock Unlock events iphone

    For the third one, i think there should be some api to detect that. you can try to search on google.

    Kit

    Like

  5. Thanks much for posting this.
    I just put it in my application, and got a great picture of everything BUT my navigation bar.
    Do you have any idea how to capture that as well?

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.