Skip to content

feat(iOS) - url function support in backgroundImage - #58248

Open
intergalacticspacehighway wants to merge 1 commit into
react:mainfrom
intergalacticspacehighway:background-image-url-ios
Open

intergalacticspacehighway wants to merge 1 commit into
react:mainfrom
intergalacticspacehighway:background-image-url-ios

Conversation

@intergalacticspacehighway

@intergalacticspacehighway intergalacticspacehighway commented Aug 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary:

Note

iOS CI fails until #58246 (JS/parser) is merged. This PR uses the URLBackgroundImage type that #58246 adds.

Recreated from #54996, which was auto-closed when my fork was deleted. GitHub does not allow reopening a PR whose head repository is gone. Rebased onto current main.

How:

  • RCTViewComponentView loads the url() images when it receives props, through the same ImageManager pipeline as the Image component.
  • RCTBackgroundImageURLLoader keeps one request per unique url. It cancels a request when its url is removed, or when the view is recycled or deallocated.
  • RCTSurfacePresenter puts one shared ImageManager in the context container, unless the app already added one. <Image> now uses this ImageManager too.
  • RCTMountingManager passes the ImageManager to RCTComponentViewRegistry, which gives it to each new RCTViewComponentView.

Changelog:

[IOS] [ADDED] - url support with backgroundImage

Test Plan:

Screenshots

Each url() example in RN Tester, opened by deep link, on the iOS simulator, with all three PRs merged:

URL Image URL Image from local file Local file at its natural size (@3x asset, 33x33)

Follow-ups

  • Decode images at the size they are drawn, instead of full size.
  • cover and contain support for backgroundSize.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 30, 2026
@intergalacticspacehighway
intergalacticspacehighway marked this pull request as draft August 30, 2026 13:05
@intergalacticspacehighway
intergalacticspacehighway force-pushed the background-image-url-ios branch 3 times, most recently from d123490 to f4d9407 Compare August 31, 2026 13:36
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at 4b60a759aa10.

ios: no capture

@intergalacticspacehighway
intergalacticspacehighway marked this pull request as ready for review October 29, 2026 14:35
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 29, 2026
@intergalacticspacehighway intergalacticspacehighway added the p: Expo Partner: Expo label Nov 4, 2026
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Nov 30, 2026

CALayer *imageLayer = [CALayer layer];
imageLayer.contents = (__bridge id)image.CGImage;
imageLayer.bounds = swapsAxes ? CGRectMake(0, 0, size.height, size.width) : CGRectMake(0, 0, size.width, size.height);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The layer never sets contentsScale, so on a Retina display the decoded image rasterizes at 1x. Could you set itemLayer.contentsScale = image.scale (and the same on the inner imageLayer)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can avoid setting it, since these layers only display an already-decoded bitmap stretched to their bounds, so contentsScale has no effect. Also, image.scale is always 1 here, so it would be a no-op.


- (void)setImageManager:(std::shared_ptr<ImageManager>)imageManager
{
_imageManager = std::move(imageManager);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recycled views keep the manager they were created with--if setImageManager: ever re-fires, pooled views would hold a stale ImageManager, putting two queues on one RCTImageLoader. Worth setting the manager on every dequeue, not just newly created views?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Today it is set only once, before any view exists, but I moved it to every dequeue so recycled views always get the current manager. Fixed in 65a1222.

meta-codesync Bot pushed a commit that referenced this pull request Dec 1, 2026
## Summary:

- Adds support for loading images with `url` syntax. e.g. `backgroundImage: url("https://example.com/1.png")`.
- `background-repeat: round` now uses at least one tile, as CSS requires. This also changes gradients whose tile is more than twice the size of the box.
- This PR only includes android code changes. To fully test the PR, merge #58246 (JS/parser) and #58248 (iOS) into this one.

> Recreated from #54994, which was auto-closed when my fork was deleted. GitHub does not allow reopening a PR whose head repository is gone. Rebased onto current `main`.

### How:
Uses Fresco's image pipeline to fetch the image in `BackgroundImageURLLoader`. Keeps Fresco's `CloseableReference` open and draws the shared bitmap in the `BackgroundImageDrawable` canvas using `drawBitmap`, like Fresco's drawee. Background images with the same url share one bitmap. The reference is released when the url changes or the view is dropped.

## Changelog:

[ANDROID] [ADDED] - `url` support with `backgroundImage`

## Test Plan:

- Merge #58246, this PR and #58248, then test the examples added in the JS PR.
- Added unit tests: `./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests 'com.facebook.react.uimanager.drawable.*' --tests 'com.facebook.react.uimanager.style.BackgroundImageLayerTest'`

### Screenshots

Each `url()` example in RN Tester, opened by deep link, on the Android emulator, with all three PRs merged:

| URL Image | URL Image from local file | Local file at its natural size (@3x asset, 33x33) |
| --- | --- | --- |
| <img src="https://raw.githubusercontent.com/intergalacticspacehighway/react-native/c7893b2cc494b383efc609a82f71b1ceda0e6411/android-1.png" width="100" /> | <img src="https://raw.githubusercontent.com/intergalacticspacehighway/react-native/c7893b2cc494b383efc609a82f71b1ceda0e6411/android-2.png" width="100" /> | <img src="https://raw.githubusercontent.com/intergalacticspacehighway/react-native/c7893b2cc494b383efc609a82f71b1ceda0e6411/android-3.png" width="100" /> |

### Follow-ups

- Decode images at the size they are drawn, instead of full size.
- `cover` and `contain` support for `backgroundSize`.

Differential Revision: D131256210

Pull Request resolved: #58247
Adds support for loading images with url() syntax on iOS, for example
`backgroundImage: url("https://example.com/1.png")`.

RCTViewComponentView loads the url() images when it receives props,
through the same ImageManager pipeline as the Image component.
RCTBackgroundImageURLLoader keeps one request per unique url, and
cancels a request when its url is removed or the view is recycled or
deallocated. background-repeat: round now uses at least one tile, as
CSS requires.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. p: Expo Partner: Expo Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants