xcode: Button to take screenshot and store it in the image picker

Posted on 2012-08-21
Last Modified: 2016-02-10
Hello Experts,

Basically I am making an app that adds specific effects to my camera application.  I have built this from the ground up, and it seems something so simple has gotten me stumped.  apple documentation on this seems dated and I can not get it to work.  I am just looking for a way to push a button to capture a screenshot of the application and store it in the image picker.  I can not find the code or/and figure out how to do it.

Thanks in advance.
Question by:thomashospital
    LVL 33

    Assisted Solution

    You can try something like this:

    - (void) makeSnapshot
        UIGraphicsBeginImageContext([self frame].size);
        [[self layer] renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
        // Save the captured image to photo album
        UIImageWriteToSavedPhotosAlbum(screenImage, self, 
                       @selector(image:didFinishSavingWithError:contextInfo:), nil);

    Open in new window

    In the button action method call:

    [self makeSnapshot];

    Open in new window

    It's taken from
    Make a snapshot from an iPhone application

    Accepted Solution

    Thank you pgnatyuk for taking the time to respond.  You were partially correct with the link and code you gave me.  I found my solution from another site that I found, however would not have found it had you not steered me in the right direction.  It looks like my problem was that I was using an EAGL View and was not passing my variables correctly.  Below is the code I used for anyone else that runs into this problem.

    - (UIImage*) getGLScreenshot {
        NSInteger myDataLength = 320 * 480 * 4;
        // allocate array and read pixels into it.
        GLubyte *buffer = (GLubyte *) malloc(myDataLength);
        glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
        // gl renders "upside down" so swap top to bottom into new array.
        // there's gotta be a better way, but this works.
        GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
        for(int y = 0; y <480; y++)
            for(int x = 0; x <320 * 4; x++)
                buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
        // make data provider with data.
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
        // prep the ingredients
        int bitsPerComponent = 8;
        int bitsPerPixel = 32;
        int bytesPerRow = 4 * 320;
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
        // make the cgimage
        CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
        // then make the uiimage from that
        UIImage *myImage = [UIImage imageWithCGImage:imageRef];
        return myImage;
    - (void)saveGLScreenshotToPhotosAlbum {
        UIImageWriteToSavedPhotosAlbum([self getGLScreenshot], nil, nil, nil);  

    Open in new window


    Author Closing Comment

    I did not state I was using a GL view, I forgot to mention that.  pgnatyuk method does work for UIImage views, when I tested it on my device, however it did not pass the variables I needed.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Ever wanted to watch videos in your computer from the bed without the need of standing up and shutting down the computer?  Is your computers operating system Windows XP, Windows Vista or Windows 7?  Do you own a iOS device? If all the answers were y…
    The Basics of Storyboard Storyboards were first introduced to developers in the release of iOS 5. They save the developer the trouble of designing each screen’s interface in a different file. The storyboard allows you to see the conceptual mockup o…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now