Browse Source

Updated iOS functionality to provide the "captureScreenshot." This will capture the entire screen all the views currently displayed. This performs a native screen capture, so a ref tag is not required.

foxmicha 7 years ago
parent
commit
72e746e642
1 changed files with 25 additions and 2 deletions
  1. 25
    2
      ios/RNViewShot.m

+ 25
- 2
ios/RNViewShot.m View File

19
   return RCTGetUIManagerQueue();
19
   return RCTGetUIManagerQueue();
20
 }
20
 }
21
 
21
 
22
+RCT_EXPORT_METHOD(captureScreenshot: (NSDictionary *)options
23
+                  resolve:(RCTPromiseResolveBlock)resolve
24
+                  reject:(RCTPromiseRejectBlock)reject) 
25
+{
26
+  [captureRef -1, options, resolve, reject];
27
+}
22
 
28
 
23
 RCT_EXPORT_METHOD(releaseCapture:(nonnull NSString *)uri)
29
 RCT_EXPORT_METHOD(releaseCapture:(nonnull NSString *)uri)
24
 {
30
 {
41
 
47
 
42
     // Get view
48
     // Get view
43
     UIView *view;
49
     UIView *view;
44
-    view = viewRegistry[target];
50
+
51
+    if (target == -1) {
52
+      UIWindow *window = [[UIApplication sharedApplication] keyWindow];
53
+      view = window.rootViewController.view;
54
+    } else {
55
+      view = viewRegistry[target];
56
+    }
57
+
45
     if (!view) {
58
     if (!view) {
46
       reject(RCTErrorUnspecified, [NSString stringWithFormat:@"No view found with reactTag: %@", target], nil);
59
       reject(RCTErrorUnspecified, [NSString stringWithFormat:@"No view found with reactTag: %@", target], nil);
47
       return;
60
       return;
88
       scrollView.contentOffset = CGPointZero;
101
       scrollView.contentOffset = CGPointZero;
89
       scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
102
       scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
90
     }
103
     }
91
-    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
104
+
105
+    if (target == -1) {
106
+      if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
107
+        UIGraphicsBeginImageContextWithOptions(currentView.window.bounds.size, NO, [UIScreen mainScreen].scale);
108
+      } else {
109
+        UIGraphicsBeginImageContext(currentView.window.bounds.size);
110
+      }
111
+    } else {
112
+      UIGraphicsBeginImageContextWithOptions(size, NO, 0);
113
+    }
114
+    
92
     success = [rendered drawViewHierarchyInRect:(CGRect){CGPointZero, size} afterScreenUpdates:YES];
115
     success = [rendered drawViewHierarchyInRect:(CGRect){CGPointZero, size} afterScreenUpdates:YES];
93
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
116
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
94
     UIGraphicsEndImageContext();
117
     UIGraphicsEndImageContext();