Gaëtan Renaudeau 7 years ago
parent
commit
86a09a8555
1 changed files with 29 additions and 5 deletions
  1. 29
    5
      ios/RNViewShot.m

+ 29
- 5
ios/RNViewShot.m View File

5
 #import <React/UIView+React.h>
5
 #import <React/UIView+React.h>
6
 #import <React/RCTUtils.h>
6
 #import <React/RCTUtils.h>
7
 #import <React/RCTConvert.h>
7
 #import <React/RCTConvert.h>
8
+#import <React/RCTScrollView.h>
8
 #import <React/RCTUIManager.h>
9
 #import <React/RCTUIManager.h>
9
 #import <React/RCTBridge.h>
10
 #import <React/RCTBridge.h>
10
 
11
 
54
     CGSize size = [RCTConvert CGSize:options];
55
     CGSize size = [RCTConvert CGSize:options];
55
     NSString *format = [RCTConvert NSString:options[@"format"] ?: @"png"];
56
     NSString *format = [RCTConvert NSString:options[@"format"] ?: @"png"];
56
     NSString *result = [RCTConvert NSString:options[@"result"] ?: @"file"];
57
     NSString *result = [RCTConvert NSString:options[@"result"] ?: @"file"];
57
-    
58
+    BOOL snapshotContentContainer = [RCTConvert BOOL:options[@"snapshotContentContainer"] ?: @"false"];
59
+
58
     // Capture image
60
     // Capture image
59
-    if (size.width < 0.1 || size.height < 0.1) {
60
-      size = view.bounds.size;
61
+    BOOL success;
62
+    if (snapshotContentContainer) {
63
+      if (![view isKindOfClass:[RCTScrollView class]]) {
64
+        reject(RCTErrorUnspecified, [NSString stringWithFormat:@"snapshotContentContainer can only be used on a RCTScrollView. instead got: %@", view], nil);
65
+        return;
66
+      }
67
+      RCTScrollView* rctScrollView = view;
68
+      UIScrollView* scrollView = rctScrollView.scrollView;
69
+      CGPoint savedContentOffset = scrollView.contentOffset;
70
+      CGRect savedFrame = scrollView.frame;
71
+      scrollView.contentOffset = CGPointZero;
72
+      scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
73
+      if (size.width < 0.1 || size.height < 0.1) {
74
+        size = scrollView.contentSize;
75
+      }
76
+      UIGraphicsBeginImageContextWithOptions(size, NO, 0);
77
+      success = [scrollView drawViewHierarchyInRect:(CGRect){CGPointZero, size} afterScreenUpdates:YES];
78
+      scrollView.contentOffset = savedContentOffset;
79
+      scrollView.frame = savedFrame;
80
+    }
81
+    else {
82
+      if (size.width < 0.1 || size.height < 0.1) {
83
+        size = view.bounds.size;
84
+      }
85
+      UIGraphicsBeginImageContextWithOptions(size, NO, 0);
86
+      success = [view drawViewHierarchyInRect:(CGRect){CGPointZero, size} afterScreenUpdates:YES];
61
     }
87
     }
62
-    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
63
-    BOOL success = [view drawViewHierarchyInRect:(CGRect){CGPointZero, size} afterScreenUpdates:YES];
64
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
88
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
65
     UIGraphicsEndImageContext();
89
     UIGraphicsEndImageContext();
66
     
90