Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take screenshot programmatically iOs 5

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!!

like image 412
Marti Serra Vivancos Avatar asked Dec 13 '22 07:12

Marti Serra Vivancos


2 Answers

- (void)drawRect:(CGRect)rect {   
     UIGraphicsBeginImageContext(self.bounds.size);
     [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();  
     UIGraphicsEndImageContext();   
}
like image 92
SimplyKiwi Avatar answered Mar 21 '23 00:03

SimplyKiwi


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.

like image 23
Trident Avatar answered Mar 20 '23 22:03

Trident