Created
October 8, 2013 21:44
-
-
Save andreafrancia/6892338 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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