Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take a screenshot programmatically in iOS?

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!

like image 937
coodi8 Avatar asked Nov 08 '12 07:11

coodi8


Video Answer


1 Answers

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;
     }
like image 142
Himanshu Mahajan Avatar answered Oct 22 '22 19:10

Himanshu Mahajan