forked from ProcursusTeam/uikittools-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uisave.m
115 lines (96 loc) · 2.85 KB
/
uisave.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
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
#import <UIKit/UIKit.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
// clang-format off
void usage() {
printf(_("Usage: %s file ...\n\
Copyright (C) 2021, Procursus Team. All Rights Reserved.\n\n"), getprogname());
printf(_("Save images and videos to the camera roll\n\n"));
printf(_("Image and video formats that can be saved to the camera roll\n\
vary between iOS versions\n\n"));
printf(_("Contact the Procursus Team for support.\n"));
}
// clang-format on
int main(int argc, char *argv[]) {
#if NLS
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
#endif
if (argc < 2) {
usage();
return 1;
}
NSMutableArray<NSString *> *images = [NSMutableArray array];
NSMutableArray<NSString *> *videos = [NSMutableArray array];
for (int i = 1; i < argc; i++) {
char *path = argv[i];
NSString *pathString =
[[NSString stringWithUTF8String:path] stringByExpandingTildeInPath];
BOOL directory;
BOOL exists =
[NSFileManager.defaultManager fileExistsAtPath:pathString
isDirectory:&directory];
if (directory || !exists) {
fprintf(stderr, _("No file at path: %s\n"), path);
return 1;
}
if (![NSURL fileURLWithPath:pathString isDirectory:NO]) {
fprintf(stderr, _("Invalid path: %s\n"), path);
return 1;
}
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(pathString)) {
[videos addObject:pathString];
} else if ([UIImage imageWithContentsOfFile:pathString]) {
// imageWithContentsOfFile returns nil if it can't create an image
// from the file
[images addObject:pathString];
} else {
fprintf(stderr, _("No supported image or video format at: %s\n"),
path);
return 1;
}
}
NSError *error;
for (NSString *image in images) {
NSURL *url = [NSURL fileURLWithPath:image isDirectory:NO];
BOOL success = [[PHPhotoLibrary sharedPhotoLibrary]
performChangesAndWait:^{
[PHAssetChangeRequest
creationRequestForAssetFromImageAtFileURL:url];
}
error:&error];
if (!success) {
const char *errorString = error.localizedDescription.UTF8String;
fprintf(stderr, _("Failed to save image at %s with error: %s\n"),
image.UTF8String, errorString);
return 1;
}
}
for (NSString *video in videos) {
NSURL *url = [NSURL fileURLWithPath:video isDirectory:NO];
BOOL success = [[PHPhotoLibrary sharedPhotoLibrary]
performChangesAndWait:^{
[PHAssetChangeRequest
creationRequestForAssetFromVideoAtFileURL:url];
}
error:&error];
if (!success) {
const char *errorString = error.localizedDescription.UTF8String;
fprintf(stderr, _("Failed to save video at %s with error: %s\n"),
video.UTF8String, errorString);
return 1;
}
}
return 0;
}