Skip to content

Instantly share code, notes, and snippets.

@andreafrancia
Created October 8, 2013 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreafrancia/6892338 to your computer and use it in GitHub Desktop.
Save andreafrancia/6892338 to your computer and use it in GitHub Desktop.
- (void)saveScreenshot
{
NSString * name = @"ciao";
BOOL includeStatusBar = NO;
//Get image with status bar cropped out
BOOL isRetina = [[UIScreen mainScreen] scale] != 1.0f;
CGFloat StatusBarHeight = isRetina ? 40 : 20;
CGImageRef CGImage = UIGetScreenImage();
BOOL isPortrait = UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]);
CGRect imageRect;
if (!includeStatusBar) {
if (isPortrait) {
imageRect = CGRectMake(0, StatusBarHeight, CGImageGetWidth(CGImage), CGImageGetHeight(CGImage) - StatusBarHeight);
} else {
imageRect = CGRectMake(StatusBarHeight, 0, CGImageGetWidth(CGImage) - StatusBarHeight, CGImageGetHeight(CGImage));
}
CGImage = (__bridge CGImageRef)CFBridgingRelease(CGImageCreateWithImageInRect(CGImage, imageRect));
}
NSString *devicePrefix = nil;
NSString *screenDensity = isRetina ? @"@2x" : @"";
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
devicePrefix = [NSString stringWithFormat:@"iphone%.0f%@", CGRectGetHeight([[UIScreen mainScreen] bounds]), screenDensity];
} else {
devicePrefix = [NSString stringWithFormat:@"ipad%@",screenDensity];
}
UIImage *image = [UIImage imageWithCGImage:CGImage];
NSData *data = UIImagePNGRepresentation(image);
NSString *file = [NSString stringWithFormat:@"%@-%@-%@.png", devicePrefix, [[NSLocale currentLocale] localeIdentifier], name];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *fileURL = [[NSURL fileURLWithPath:documentsPath] URLByAppendingPathComponent:file];
NSLog(@"Saving screenshot: %@", [fileURL path]);
[data writeToURL:fileURL atomically:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment