• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1956
  • Last Modified:

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

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.
0
thomashospital
Asked:
thomashospital
  • 2
2 Solutions
 
pgnatyukCommented:
You can try something like this:

- (void) makeSnapshot
{
    UIGraphicsBeginImageContext([self frame].size);
    [[self layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // 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
http://www.wmdeveloper.com/2010/09/make-snapshot-from-iphone-application.html
0
 
thomashospitalAuthor Commented:
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

0
 
thomashospitalAuthor Commented:
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now