|
@@ -42,7 +42,7 @@ RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber *)target
|
42
|
42
|
reject:(RCTPromiseRejectBlock)reject)
|
43
|
43
|
{
|
44
|
44
|
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
|
45
|
|
-
|
|
45
|
+
|
46
|
46
|
// Get view
|
47
|
47
|
UIView *view;
|
48
|
48
|
view = viewRegistry[target];
|
|
@@ -50,16 +50,16 @@ RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber *)target
|
50
|
50
|
reject(RCTErrorUnspecified, [NSString stringWithFormat:@"No view found with reactTag: %@", target], nil);
|
51
|
51
|
return;
|
52
|
52
|
}
|
53
|
|
-
|
|
53
|
+
|
54
|
54
|
// Get options
|
55
|
55
|
CGSize size = [RCTConvert CGSize:options];
|
56
|
56
|
NSString *format = [RCTConvert NSString:options[@"format"] ?: @"png"];
|
57
|
|
- NSString *result = [RCTConvert NSString:options[@"result"] ?: @"file"];
|
|
57
|
+ NSString *result = [RCTConvert NSString:options[@"result"] ?: @"tmpfile"];
|
58
|
58
|
BOOL snapshotContentContainer = [RCTConvert BOOL:options[@"snapshotContentContainer"] ?: @"false"];
|
59
|
59
|
|
60
|
60
|
// Capture image
|
61
|
61
|
BOOL success;
|
62
|
|
-
|
|
62
|
+
|
63
|
63
|
UIView* rendered;
|
64
|
64
|
UIScrollView* scrollView;
|
65
|
65
|
if (snapshotContentContainer) {
|
|
@@ -74,7 +74,7 @@ RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber *)target
|
74
|
74
|
else {
|
75
|
75
|
rendered = view;
|
76
|
76
|
}
|
77
|
|
-
|
|
77
|
+
|
78
|
78
|
if (size.width < 0.1 || size.height < 0.1) {
|
79
|
79
|
size = snapshotContentContainer ? scrollView.contentSize : view.bounds.size;
|
80
|
80
|
}
|
|
@@ -82,7 +82,7 @@ RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber *)target
|
82
|
82
|
reject(RCTErrorUnspecified, [NSString stringWithFormat:@"The content size must not be zero or negative. Got: (%g, %g)", size.width, size.height], nil);
|
83
|
83
|
return;
|
84
|
84
|
}
|
85
|
|
-
|
|
85
|
+
|
86
|
86
|
CGPoint savedContentOffset;
|
87
|
87
|
CGRect savedFrame;
|
88
|
88
|
if (snapshotContentContainer) {
|
|
@@ -96,13 +96,13 @@ RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber *)target
|
96
|
96
|
success = [rendered drawViewHierarchyInRect:(CGRect){CGPointZero, size} afterScreenUpdates:YES];
|
97
|
97
|
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
|
98
|
98
|
UIGraphicsEndImageContext();
|
99
|
|
-
|
|
99
|
+
|
100
|
100
|
if (snapshotContentContainer) {
|
101
|
101
|
// Restore scroll & frame
|
102
|
102
|
scrollView.contentOffset = savedContentOffset;
|
103
|
103
|
scrollView.frame = savedFrame;
|
104
|
104
|
}
|
105
|
|
-
|
|
105
|
+
|
106
|
106
|
if (!success) {
|
107
|
107
|
reject(RCTErrorUnspecified, @"The view cannot be captured. drawViewHierarchyInRect was not successful. This is a potential technical or security limitation.", nil);
|
108
|
108
|
return;
|
|
@@ -112,10 +112,10 @@ RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber *)target
|
112
|
112
|
reject(RCTErrorUnspecified, @"Failed to capture view snapshot. UIGraphicsGetImageFromCurrentImageContext() returned nil!", nil);
|
113
|
113
|
return;
|
114
|
114
|
}
|
115
|
|
-
|
|
115
|
+
|
116
|
116
|
// Convert image to data (on a background thread)
|
117
|
117
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
118
|
|
-
|
|
118
|
+
|
119
|
119
|
NSData *data;
|
120
|
120
|
if ([format isEqualToString:@"png"]) {
|
121
|
121
|
data = UIImagePNGRepresentation(image);
|
|
@@ -126,13 +126,13 @@ RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber *)target
|
126
|
126
|
reject(RCTErrorUnspecified, [NSString stringWithFormat:@"Unsupported image format: %@. Try one of: png | jpg | jpeg", format], nil);
|
127
|
127
|
return;
|
128
|
128
|
}
|
129
|
|
-
|
|
129
|
+
|
130
|
130
|
NSError *error = nil;
|
131
|
131
|
NSString *res = nil;
|
132
|
|
- if ([result isEqualToString:@"file"]) {
|
|
132
|
+ if ([result isEqualToString:@"file"] || [result isEqualToString:@"tmpfile"] ) {
|
133
|
133
|
// Save to a temp file
|
134
|
134
|
NSString *path;
|
135
|
|
- if (options[@"path"]) {
|
|
135
|
+ if (options[@"path"] && [result isEqualToString:@"file"]) {
|
136
|
136
|
path = options[@"path"];
|
137
|
137
|
NSString * folder = [path stringByDeletingLastPathComponent];
|
138
|
138
|
NSFileManager * fm = [NSFileManager defaultManager];
|
|
@@ -167,7 +167,7 @@ RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber *)target
|
167
|
167
|
resolve(res);
|
168
|
168
|
return;
|
169
|
169
|
}
|
170
|
|
-
|
|
170
|
+
|
171
|
171
|
// If we reached here, something went wrong
|
172
|
172
|
if (error) reject(RCTErrorUnspecified, error.localizedDescription, error);
|
173
|
173
|
else reject(RCTErrorUnspecified, @"viewshot unknown error", nil);
|