Repository navigation
fix(ios): resize useRenderInContext captures to width/height - #730
Merged
Merged
Conversation
-[CALayer renderInContext:] draws the layer at its own size, so with useRenderInContext a width/height smaller than the view cropped the capture and a larger one padded it with transparent pixels. Scale the context to the requested size, as drawViewHierarchyInRect: does.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With
useRenderInContext: true,width/heightset the output size but do not resize the view:-[CALayer renderInContext:]draws the layer at its own size. A smaller size crops the capture to its top-left part; a larger size pads it with transparent pixels. The defaultdrawViewHierarchyInRect:path resizes.On a 200x100 view with four coloured quadrants (iOS simulator, example app):
Fix
When
width/heightdiffer from the natural capture size (the view's bounds, or the content size withsnapshotContentContainer), scale the context from the natural size to the requested size beforerenderInContext:. Captures withoutwidth/heightare unchanged.Tests
RNViewShotRenderInContextTests(ViewShotExampleTests) callscaptureRef:withOptions:with a stub UI manager and checks the image size and quadrant colours. The four resize cases (smaller, larger,snapshotContentContainer, transformed scroll view withsnapshotContentContainer) fail on master and pass with the fix. The two natural-size cases (plain view, transformed scroll view) pass on both. The existing 14 tests pass.From JS in the example app (iPhone 17 Pro simulator, iOS 26.3),
useRenderInContextcaptures are now pixel-identical to the default strategy at natural size, 100x50 and 400x200. On master they match only at natural size.Not run locally: Android, Windows, SwiftPM and Detox jobs (no change outside
ios/RNViewShot.mmand the iOS test target).#696 also edits this
renderInContextbranch (sublayer order); the two changes are independent.