Преглед на файлове

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 години
родител
ревизия
72e746e642
променени са 1 файла, в които са добавени 25 реда и са изтрити 2 реда
  1. 25
    2
      ios/RNViewShot.m

+ 25
- 2
ios/RNViewShot.m Целия файл

@@ -19,6 +19,12 @@ RCT_EXPORT_MODULE()
19 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 29
 RCT_EXPORT_METHOD(releaseCapture:(nonnull NSString *)uri)
24 30
 {
@@ -41,7 +47,14 @@ RCT_EXPORT_METHOD(captureRef:(nonnull NSNumber *)target
41 47
 
42 48
     // Get view
43 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 58
     if (!view) {
46 59
       reject(RCTErrorUnspecified, [NSString stringWithFormat:@"No view found with reactTag: %@", target], nil);
47 60
       return;
@@ -88,7 +101,17 @@ RCT_EXPORT_METHOD(captureRef:(nonnull NSNumber *)target
88 101
       scrollView.contentOffset = CGPointZero;
89 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 115
     success = [rendered drawViewHierarchyInRect:(CGRect){CGPointZero, size} afterScreenUpdates:YES];
93 116
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
94 117
     UIGraphicsEndImageContext();