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

Leave a Comment

Copyright © 2019 Soulmedic Theme All Rights Reserved | Design Themes

Atomic Flavor