Skip to content

fix(ios): render snapshots at the screen's scale - #4459

Closed
mickeypause wants to merge 2 commits into
rnmapbox:mainfrom
mickeypause:feat/snapshot-pixel-ratio-attribution
Closed

mickeypause wants to merge 2 commits into
rnmapbox:mainfrom
mickeypause:feat/snapshot-pixel-ratio-attribution

Conversation

@mickeypause

@mickeypause mickeypause commented Nov 26, 2026 •

Copy link
Copy Markdown

Description

Fixes blurry snapshotManager.takeSnap images on iOS.

RNMBXSnapshotModule.swift hard-codes pixelRatio: 1.0, so every iOS snapshot is 1×. A 300 × 150 pt snapshot is only 300 × 150 px, and it looks blurry on a 2× or 3× screen. This uses UIScreen.main.scale, as RNMBXOfflineModuleLegacy already does. Android already renders at the screen density, so the two platforms now match. Android rounds the density down; iPhone scales are whole numbers anyway.

Crash: Mapbox's 8192 px limit

MapSnapshotOptions.init (Mapbox Maps SDK, MapSnapshotOptions.swift) has precondition(size.width * pixelRatio <= 8192) and the same for height. A failed precondition stops the app, in release builds too, so it can't be caught and turned into a rejected promise. Rendering at 3× would make snapshots that work today crash, so the scale is capped at 8192 / max(width, height), but never below 1× (the current behaviour):

Snapshot side (3× device) main Screen scale, no cap This PR
up to ~2730 pt 1× (blurry) 3× 3×
~2730 – 8192 pt 1× crash between 1× and 3×
over 8192 pt crash crash crash (unchanged)

So the screen scale is used whenever it fits, and anything that works on main still works. Known limitation, not changed by this PR: a snapshot with a side over 8192 pt still crashes, as it does on main today, because even 1× is over the limit. Fixing that means either rendering below 1× or rejecting the call, which is a separate behaviour decision.

Crash seen without the cap (TakeSnapshot scene temporarily set to 3000 pt wide, iPhone 16 Pro simulator):

Exception: EXC_BREAKPOINT (SIGTRAP)
0  libswiftCore.dylib  _assertionFailure(_:_:file:line:flags:)
1  MapboxMaps          MapSnapshotOptions.init(size:pixelRatio:glyphsRasterizationOptions:showsLogo:showsAttribution:scaleFactor:)
2  RNMapboxExample     RNMBXSnapshotModule._getSnapshotterOptions(_:)
3  RNMapboxExample     closure #1 in closure #1 in RNMBXSnapshotModule.takeSnap(_:resolver:rejecter:)
4  RNMapboxExample     partial apply for closure #1 in closure #1 in RNMBXSnapshotModule.takeSnap(_:resolver:rejecter:)
5  RNMapboxExample     logged<A>(_:info:level:rejecter:fn:)

With the cap the same scene renders and no crash report is written.

On a 3× device the snapshot's pixel size grows from 300 × 150 to 900 × 450, so files and base64 strings get bigger. The size in points doesn't change.

Screenshots

The first snapshot in the TakeSnapshot example scene, unchanged. Before: main. After: this branch (at this size the scale is 3×; the 8192 px cap doesn't apply). iPhone 16 Pro simulator (3×), iOS 27, New Architecture.

Before (main: 1×) After (this branch: 3×)

Checklist

  • I've read CONTRIBUTING.md
  • I updated the doc/other generated code with running yarn generate in the root folder (no changes)
  • I have tested the new feature on /example app.
    • In V11 mode/ios (the example app is v11 + New Architecture only)
    • In New Architecture mode/ios
    • In V11 mode/android (n/a: iOS-only change)
    • In New Architecture mode/android (n/a: iOS-only change)
  • I added/updated a sample - if a new feature was implemented (/example) (n/a: bug fix, no new feature)

Testing

  • /example app: I ran the TakeSnapshot scene on an iPhone 16 Pro simulator (iOS 27, New Architecture), with and without this change. The screenshots above come from those runs.
  • Large snapshots: with the TakeSnapshot scene temporarily set to 3000 pt wide, the build without the cap crashed (EXC_BREAKPOINT in MapSnapshotOptions.init, called from _getSnapshotterOptions). With the cap, the snapshots render. That scene edit isn't part of this PR.
  • Android: not affected.

Component to reproduce the issue you're fixing

On main, on a 2× or 3× iPhone, the snapshot is blurry. With this branch it's sharp.

Reproducer
import React, { useEffect, useState } from 'react';
import { Image, View } from 'react-native';
import { snapshotManager } from '@rnmapbox/maps';

const BugReportExample = () => {
  const [uri, setUri] = useState(null);

  useEffect(() => {
    snapshotManager
      .takeSnap({
        centerCoordinate: [-74.12641, 40.797968],
        zoomLevel: 13,
        width: 300,
        height: 150,
      })
      .then(setUri);
  }, []);

  return (
    <View style={{ flex: 1, padding: 16 }}>
      {uri && <Image source={{ uri }} style={{ width: 300, height: 150 }} />}
    </View>
  );
};

export default BugReportExample;

@mickeypause
mickeypause force-pushed the feat/snapshot-pixel-ratio-attribution branch from 5c478e4 to 233cce5 Compare November 27, 2026 08:40
@mickeypause mickeypause changed the title feat(snapshot): add pixelRatio and withAttribution options feat(snapshot): add pixelRatio option Nov 27, 2026
@mickeypause
mickeypause marked this pull request as draft November 27, 2026 08:43
RNMBXSnapshotModule hard-coded pixelRatio: 1.0, so every iOS snapshot
was 1x and looked blurry when shown at its point size on a 2x or 3x
screen. Use UIScreen.main.scale, as the offline module already does.
Android already renders at the screen density.
@mickeypause
mickeypause force-pushed the feat/snapshot-pixel-ratio-attribution branch from 233cce5 to 5251ee8 Compare November 27, 2026 09:32
mickeypause added a commit to mickeypause/maps that referenced this pull request Nov 27, 2026
@mickeypause mickeypause changed the title feat(snapshot): add pixelRatio option fix(ios): render snapshots at the screen's scale Nov 27, 2026
mickeypause added a commit to mickeypause/maps that referenced this pull request Nov 27, 2026
@mickeypause
mickeypause force-pushed the feat/snapshot-pixel-ratio-attribution branch 2 times, most recently from fe80bd8 to 28b3115 Compare November 27, 2026 10:10
MapSnapshotOptions has a precondition that each side is at most 8192 px,
so rendering at the screen's scale would crash a 3x device for snapshots
wider or taller than about 2730 pt. Lower the scale for those, but never
below 1x: snapshots with a side over 8192 pt still crash, as on main.
@mickeypause
mickeypause force-pushed the feat/snapshot-pixel-ratio-attribution branch from 28b3115 to 67f1741 Compare November 27, 2026 10:16
@mickeypause

Copy link
Copy Markdown
Author

Superseded by #4462: same change and description, from a branch whose name matches the title.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant