Skip to content

Commit 7e7dedc

Browse files
committed
iOS: share more code + better error for zero-ed content size
fixes #21
1 parent 998fd0c commit 7e7dedc

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

ios/RNViewShot.m

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,46 @@ - (NSDictionary *)constantsToExport
5959

6060
// Capture image
6161
BOOL success;
62+
63+
UIView* rendered;
64+
UIScrollView* scrollView;
6265
if (snapshotContentContainer) {
6366
if (![view isKindOfClass:[RCTScrollView class]]) {
6467
reject(RCTErrorUnspecified, [NSString stringWithFormat:@"snapshotContentContainer can only be used on a RCTScrollView. instead got: %@", view], nil);
6568
return;
6669
}
6770
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;
7192
scrollView.contentOffset = CGPointZero;
7293
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
7899
scrollView.contentOffset = savedContentOffset;
79100
scrollView.frame = savedFrame;
80101
}
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-
}
88102
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
89103
UIGraphicsEndImageContext();
90104

0 commit comments

Comments
 (0)