Browse Source

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 years ago
parent
commit
7e7dedc71a
1 changed files with 29 additions and 15 deletions
  1. 29
    15
      ios/RNViewShot.m

+ 29
- 15
ios/RNViewShot.m View File

59
 
59
 
60
     // Capture image
60
     // Capture image
61
     BOOL success;
61
     BOOL success;
62
+    
63
+    UIView* rendered;
64
+    UIScrollView* scrollView;
62
     if (snapshotContentContainer) {
65
     if (snapshotContentContainer) {
63
       if (![view isKindOfClass:[RCTScrollView class]]) {
66
       if (![view isKindOfClass:[RCTScrollView class]]) {
64
         reject(RCTErrorUnspecified, [NSString stringWithFormat:@"snapshotContentContainer can only be used on a RCTScrollView. instead got: %@", view], nil);
67
         reject(RCTErrorUnspecified, [NSString stringWithFormat:@"snapshotContentContainer can only be used on a RCTScrollView. instead got: %@", view], nil);
65
         return;
68
         return;
66
       }
69
       }
67
       RCTScrollView* rctScrollView = view;
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
       scrollView.contentOffset = CGPointZero;
92
       scrollView.contentOffset = CGPointZero;
72
       scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
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
       scrollView.contentOffset = savedContentOffset;
99
       scrollView.contentOffset = savedContentOffset;
79
       scrollView.frame = savedFrame;
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
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
102
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
89
     UIGraphicsEndImageContext();
103
     UIGraphicsEndImageContext();
90
     
104