4

I'm developing an iOs 5 iPhone app, I would like to take a screenshot programmatically when the user press a button of a view in the story board. I've tried many codes but they are for older versions of iOs. How can I do it in iOs 5. Thanks!!

Marti Serra Vivancos
  • 1,195
  • 5
  • 17
  • 33
  • possible duplicate of [How to take a screenshot programmatically](http://stackoverflow.com/questions/2200736/how-to-take-a-screenshot-programmatically) – 一二三 Nov 27 '11 at 13:46
  • 1
    @– 一二三 , the post clearly says that user wants it for iOS5 – Satyam Dec 30 '11 at 12:46

3 Answers3

12
- (void)drawRect:(CGRect)rect {   
     UIGraphicsBeginImageContext(self.bounds.size);
     [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();  
     UIGraphicsEndImageContext();   
}
Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
1

Well, there are few ways of capturing the iPhone screen programmatically

  1. Using UIKIT http://developer.apple.com/library/ios/#qa/qa1703/_index.html

  2. Using AVFoundation framework http://developer.apple.com/library/ios/#qa/qa1702/_index.html

  3. Using OpenGL ES http://developer.apple.com/library/ios/#qa/qa1704/_index.html

  4. Using view

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContext(screenRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);
    [self.window.layer renderInContext:ctx];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
  5. Using Window

    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

Out of these 5 methods, I found the first option very convenient for copy-pasting and for applying level of compression as 1st method gives the image with true pixel data.

Trident
  • 810
  • 9
  • 20
0

Since this is an old question, I put the code here, hope help someone. (The code is copy from Apple: http://developer.apple.com/library/ios/#qa/qa1703/_index.html)

- (UIImage*)screenshot 
{
    // Create a graphics context with the target size
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    else
        UIGraphicsBeginImageContext(imageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Iterate over every window from back to front
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) 
    {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
        {
            // -renderInContext: renders in the coordinate space of the layer,
            // so we must first apply the layer's geometry to the graphics context
            CGContextSaveGState(context);
            // Center the context around the window's anchor point
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            // Apply the window's transform about the anchor point
            CGContextConcatCTM(context, [window transform]);
            // Offset by the portion of the bounds left of and above the anchor point
            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);

            // Render the layer hierarchy to the current context
            [[window layer] renderInContext:context];

            // Restore the context
            CGContextRestoreGState(context);
        }
    }

    // Retrieve the screenshot image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}
ZYiOS
  • 5,204
  • 3
  • 39
  • 45