forked from ksuther/KSScreenshotManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKSScreenshotManager.m
159 lines (124 loc) · 5.55 KB
/
KSScreenshotManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* KSScreenshotManager.m
*
* Copyright (c) 2013 Kent Sutherland
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#if CREATING_SCREENSHOTS
#import "KSScreenshotManager.h"
#import "KSScreenshotAction.h"
CGImageRef UIGetScreenImage(); //private API for getting an image of the entire screen
@interface KSScreenshotManager ()
@property(nonatomic, strong) NSMutableArray *screenshotActions;
@end
@implementation KSScreenshotManager
- (id)init
{
if ( (self = [super init]) ) {
NSArray *arguments = [[NSProcessInfo processInfo] arguments];
//Prefer taking the last launch argument. This allows us to specify an output path when running with WaxSim.
if ([arguments count] > 1) {
NSString *savePath = [[arguments lastObject] stringByExpandingTildeInPath];
[self setScreenshotsURL:[NSURL fileURLWithPath:savePath]];
} else {
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
[self setScreenshotsURL:[NSURL fileURLWithPath:documentsPath]];
}
}
return self;
}
- (void)takeScreenshots
{
[self setupScreenshotActions];
if ([[self screenshotActions] count] == 0) {
[NSException raise:NSInternalInconsistencyException format:@"No screenshot actions have been defined. Unable to take screenshots."];
}
[self takeNextScreenshot];
}
- (void)takeNextScreenshot
{
if ([[self screenshotActions] count] > 0) {
KSScreenshotAction *nextAction = [[self screenshotActions] objectAtIndex:0];
if ([nextAction actionBlock]) {
[nextAction actionBlock]();
}
if (![nextAction asynchronous]) {
//synchronous actions can run immediately
//asynchronous actions need to call actionIsReady manually
[self actionIsReady];
}
} else {
exit(0);
}
}
- (void)actionIsReady
{
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false); //spin the run loop to give the UI a chance to catch up
KSScreenshotAction *nextAction = [[self screenshotActions] objectAtIndex:0];
[self saveScreenshot:[nextAction name] includeStatusBar:[nextAction includeStatusBar]];
if ([nextAction cleanupBlock]) {
[nextAction cleanupBlock]();
}
[[self screenshotActions] removeObjectAtIndex:0];
[self takeNextScreenshot];
}
- (void)setupScreenshotActions
{
[NSException raise:NSInternalInconsistencyException format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)];
}
- (void)addScreenshotAction:(KSScreenshotAction *)screenshotAction
{
if (!_screenshotActions) {
[self setScreenshotActions:[NSMutableArray array]];
}
[screenshotAction setManager:self];
[[self screenshotActions] addObject:screenshotAction];
}
- (void)saveScreenshot:(NSString *)name includeStatusBar:(BOOL)includeStatusBar
{
//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];
NSURL *fileURL = [[self screenshotsURL] URLByAppendingPathComponent:file];
NSLog(@"Saving screenshot: %@", [fileURL path]);
[data writeToURL:fileURL atomically:YES];
}
@end
#endif