Sfoglia il codice sorgente

iOS: share more code + better error for zero-ed content size

fixes https://github.com/gre/react-native-view-shot/issues/21
Gaëtan Renaudeau 7 anni fa
parent
commit
7e7dedc71a
1 ha cambiato i file con 29 aggiunte e 15 eliminazioni
  1. 29
    15
      ios/RNViewShot.m

+ 29
- 15
ios/RNViewShot.m Vedi File

@@ -59,32 +59,46 @@ RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber *)target
59 59
 
60 60
     // Capture image
61 61
     BOOL success;
62
+    
63
+    UIView* rendered;
64
+    UIScrollView* scrollView;
62 65
     if (snapshotContentContainer) {
63 66
       if (![view isKindOfClass:[RCTScrollView class]]) {
64 67
         reject(RCTErrorUnspecified, [NSString stringWithFormat:@"snapshotContentContainer can only be used on a RCTScrollView. instead got: %@", view], nil);
65 68
         return;
66 69
       }
67 70
       RCTScrollView* rctScrollView = view;
68
-      UIScrollView* scrollView = rctScrollView.scrollView;
69
-      CGPoint savedContentOffset = scrollView.contentOffset;
70
-      CGRect savedFrame = scrollView.frame;
71
+      scrollView = rctScrollView.scrollView;
72
+      rendered = scrollView;
73
+    }
74
+    else {
75
+      rendered = view;
76
+    }
77
+    
78
+    if (size.width < 0.1 || size.height < 0.1) {
79
+      size = snapshotContentContainer ? scrollView.contentSize : view.bounds.size;
80
+    }
81
+    if (size.width < 0.1 || size.height < 0.1) {
82
+      reject(RCTErrorUnspecified, [NSString stringWithFormat:@"The content size must not be zero or negative. Got: (%g, %g)", size.width, size.height], nil);
83
+      return;
84
+    }
85
+    
86
+    CGPoint savedContentOffset;
87
+    CGRect savedFrame;
88
+    if (snapshotContentContainer) {
89
+      // Save scroll & frame and set it temporarily to the full content size
90
+      savedContentOffset = scrollView.contentOffset;
91
+      savedFrame = scrollView.frame;
71 92
       scrollView.contentOffset = CGPointZero;
72 93
       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];
94
+    }
95
+    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
96
+    success = [rendered drawViewHierarchyInRect:(CGRect){CGPointZero, size} afterScreenUpdates:YES];
97
+    if (snapshotContentContainer) {
98
+      // Restore scroll & frame
78 99
       scrollView.contentOffset = savedContentOffset;
79 100
       scrollView.frame = savedFrame;
80 101
     }
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];
87
-    }
88 102
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
89 103
     UIGraphicsEndImageContext();
90 104