iOS: How to take a screenshot programmatically

Considering retina display use below code :
#import  


if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(self.window.bounds.size);

[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * imgData = UIImagePNGRepresentation(image);
if(imgData)
    [imgData writeToFile:@"screenshot.png" atomically:YES];
else
    NSLog(@"error while taking screenshot");
Don't forget to do #import
iOS: How to take a screenshot programmatically iOS: How to take a screenshot programmatically Reviewed by Ricardo Castellanos on 14:12 Rating: 5

No hay comentarios:

Con la tecnología de Blogger.