forked from ProcursusTeam/uikittools-ng
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuishoot.m
160 lines (137 loc) · 3.8 KB
/
uishoot.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
160
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
#import <UIKit/UIKit.h>
#include <err.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#if NLS
# include <libintl.h>
# define _(a) gettext(a)
# define PACKAGE "uikittools-ng"
#else
# define _(a) a
#endif
#ifndef LOCALEDIR
# define LOCALEDIR "/usr/share/locale"
#endif
OBJC_EXTERN UIImage* _UICreateScreenUIImage(void);
// clang-format off
void usage(uint8_t ret) {
fprintf(stderr, _("Usage: %s [-cp] [-d number] [-f [png | jpeg | heic]] [-o file]\n"), getprogname());
exit(ret);
}
// clang-format on
int main(int argc, char** argv) {
#if NLS
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
#endif
bool copyToClipboard = false;
bool saveToPhotos = false;
long long int delay = 0;
NSString* filePath = nil;
NSString* imageFormat = @"png";
int c;
const char* errstr;
__block int ret = 0;
// clang-format off
static struct option longopts[] = {
{"clipboard", no_argument, NULL, 'b'},
{"photos", no_argument, NULL, 'p'},
{"delay", required_argument, NULL, 'd'},
{"format", required_argument, NULL, 'f'},
{"output", required_argument, NULL, 'o'},
{NULL, 0, NULL, 0}};
// clang-format on
while ((c = getopt_long(argc, argv, "cpd:f:o:", longopts, NULL)) != -1) {
switch (c) {
case 'c':
copyToClipboard = true;
break;
case 'o':
filePath = [NSString stringWithUTF8String:optarg];
break;
case 'p':
saveToPhotos = true;
break;
case 'd':
delay = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL)
err(1, "%s", optarg);
break;
case 'f':
imageFormat = [NSString stringWithUTF8String:optarg];
if (imageFormat && ![imageFormat isEqualToString:@"png"] &&
![imageFormat isEqualToString:@"jpeg"] &&
![imageFormat isEqualToString:@"heic"]) {
fprintf(stderr, _("Invalid image format '%s'\n"),
imageFormat.UTF8String);
usage(2);
}
break;
case '?':
usage(1);
break;
default:
usage(1);
}
}
if (optind <= 1) usage(1);
if (delay) sleep(delay);
UIImage* screenShot = _UICreateScreenUIImage();
if (!screenShot) {
fprintf(stderr, _("Could not capture screenshot!\n"));
return 2;
}
// tvOS doesn't have a clipboard, make this a no-op
#if !TARGET_OS_TV
if (copyToClipboard) {
FILE* old_stderr = stderr;
stderr = fopen("/dev/null", "w");
[UIPasteboard generalPasteboard].image = screenShot;
fclose(stderr);
stderr = old_stderr;
}
#endif
if (filePath) {
NSError* error;
NSString* imageUTI =
[NSString stringWithFormat:@"public.%@", imageFormat];
NSMutableData* imageData = [[NSMutableData alloc] init];
CGImageDestinationRef destinationRef = CGImageDestinationCreateWithData(
(CFMutableDataRef)imageData, (CFStringRef)imageUTI, 1, NULL);
CGImageDestinationAddImage(destinationRef, screenShot.CGImage, NULL);
if (!CGImageDestinationFinalize(destinationRef)) {
fprintf(stderr, _("Could not get image data to write to file!\n"));
ret = 3;
}
CFRelease(destinationRef);
if (ret != 3 && ![imageData writeToFile:filePath
options:NSDataWritingAtomic
error:&error]) {
fprintf(stderr, _("Could not write image to %s: %s\n"),
filePath.UTF8String, error.localizedDescription.UTF8String);
ret = 3;
}
}
if (saveToPhotos) {
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^{
[PHAssetChangeRequest
creationRequestForAssetFromImage:screenShot];
}
completionHandler:^(BOOL success, NSError* error) {
if (!success) {
fprintf(stderr, _("Could not save screenshot to Photos: %s\n"),
error.localizedDescription.UTF8String);
ret = 3;
}
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
return ret;
}