How to take a screenshot programmatically in iOS?

16,117

Solution 1

use following code to take screen shot

    -(UIImage *) screenshot
     {

          CGRect rect;
          rect=CGRectMake(0, 0, 320, 480);
          UIGraphicsBeginImageContext(rect.size);

          CGContextRef context=UIGraphicsGetCurrentContext();
          [self.view.layer renderInContext:context];

          UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();

          return image;
     }

Solution 2

-(UIImage *) screenshot
{
CGImageRef UIGetScreenImage(void);
CGImageRef screen = UIGetScreenImage();
UIImage* screenImage = [UIImage imageWithCGImage:screen];
CGImageRelease(screen); // you need to call this.
return screenImage;
}

Solution 3

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. Starting from iOS 7 you can also use Why does my programmatically created screenshot look so bad on iOS 7?

  5. Using UIWindow

    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();
    
  6. Using UIView

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

Out of these 6 options, 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. I also like option 4, as the API comes with SDK.

Solution 4

You get result Uiimage without lose quality of image

    - (UIImage *)captureView:(UIView *)view {
     UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
     [view.layer renderInContext:UIGraphicsGetCurrentContext()];
     UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return img;
     }

Solution 5

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = [UIImagePNGRepresentation(image) retain];
UImage *screenShot = [UIImage imageWithData:data];
Share:
16,117
coodi8
Author by

coodi8

Updated on July 23, 2022

Comments

  • coodi8
    coodi8 almost 2 years

    I have a UIView that uses both UIKit control and OpenGL. I'd like to get a screenshot of that view programatically.

    If I use UIGraphicsGetImageFromCurrentImageContext(), the OpenGL content is blank; If I use the glReadPixels(...) method, the UIKit content is blank;

    I'm confused as how to take a complete screenshot. Thank you!

  • coodi8
    coodi8 over 11 years
    it doesn't take effects in an OpenGL page
  • coodi8
    coodi8 over 11 years
    it only take effects in UIKit condition, for OpenGL page, only get blank image
  • coodi8
    coodi8 over 11 years
    it only take effects in UIKit condition, for OpenGL page, only get blank image
  • coodi8
    coodi8 over 11 years
    I'm developing a screenshot control, I don't know whether the view is implemented by UIKit or OpenGL, or mixed. If I can get the info that the view is implemented by what, the problem will be solved.
  • Felix K.
    Felix K. over 11 years
    @coodi8 I think this could be a problem. Won't be easy because OpenGL renders directly on the graphic chip. UI-Graphics are not rendered on the graphics chip. ( AFAIK )
  • slycrel
    slycrel over 10 years
    Thanks for this, it seems that screens with sub-view controllers don't auto-add child view controller layers. But using the window seems to work for a full-screen image. Thanks!
  • ZYiOS
    ZYiOS over 10 years
    Can someone explain how it works? Also need to fix the orientation
  • Matteo Gobbi
    Matteo Gobbi almost 10 years
    This code is just for take UIKit stuff...The question is more complex and well explained: he need to take also OpenGL stuff.
  • Julian
    Julian over 8 years
    definitely not the way to go this is why