> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Images

All remote / cacheable images go through the shared `<ImageCache>` wrapper in [src/components/ImageCache.tsx](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/components/ImageCache.tsx). The renderer underneath is [`expo-image`](https://docs.expo.dev/versions/latest/sdk/image/), which gives us disk + memory caching, recycling keys, transitions, and prefetch — none of which `react-native`'s `Image` provides out of the box.

## Why a wrapper

- **Decoupled API surface.** Screens import `@components/ImageCache` only. The day we swap to a different renderer (Fast Image, the New-Architecture image, an in-house native module) the renderer-specific types stay inside one file — no rename churn across features.
- **Drop-in for `react-native` `Image`.** `ImageCacheProps` extends `Omit<ImageProps, ...>` so existing patterns (`source={{ uri }}`, `style`, `resizeMode`, `onError`) keep working. Remote-→-local fallbacks (e.g. `setPreferRemote(false)` when a CDN URL 404s) continue to fire from `onError`.
- **Memory cache.** Call `ImageCache.clearMemoryCache()` from debug menus if needed; there is no automatic OS memory-warning hook.

## Usage

Replace `react-native`'s `Image` for remote / large images:

```tsx
// before
import { Image } from 'react-native';
<Image source={{ uri: track.coverUrl }} style={styles.cover} />

// after
import { ImageCache } from '@components/ImageCache';
<ImageCache source={{ uri: track.coverUrl }} style={styles.cover} />
```

The conversion between RN's `resizeMode` and expo-image's `contentFit` is automatic:

| `resizeMode` (RN)     | `contentFit` (expo-image) |
| --------------------- | ------------------------- |
| `contain` _(default)_ | `contain`                 |
| `cover`               | `cover`                   |
| `stretch`             | `fill`                    |
| `repeat`              | `cover`                   |

## Forwarded expo-image props

On top of every standard `ImageProps` field, these expo-image extras are exposed via `ImageCacheProps`:

| Prop                   | Purpose                                                                                        |
| ---------------------- | ---------------------------------------------------------------------------------------------- |
| `transition`           | Cross-fade duration in ms when the source changes.                                             |
| `cachePolicy`          | `'none' \| 'disk' \| 'memory' \| 'memory-disk'` (default `'disk'`).                            |
| `priority`             | `'low' \| 'normal' \| 'high'` — bias decode order for above-the-fold UI.                       |
| `recyclingKey`         | Stable id for recycled list cells (Legend List) so a re-bound cell never shows the old image.  |
| `decodeFormat`         | Android only — `'argb'` (default) or `'rgb'` (16-bit, no alpha; lower memory).                 |
| `enforceEarlyResizing` | iOS only — resize to container size before decode to cut memory on oversized sources.          |
| `useAppleWebpCodec`    | iOS only — `true` (default) uses Apple's WebP codec; `false` uses libwebp for spec compliance. |
| `onLoad`               | Fired after the asset is decoded + rendered.                                                   |
| `onError`              | Forwarded — keeps existing fallback flows working.                                             |

Any other expo-image prop that we haven't surfaced yet should be added to `ForwardedExpoImageProps` in [ImageCache.tsx](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/components/ImageCache.tsx) (not consumed via a direct `expo-image` import elsewhere).

## Static helpers

`ImageCache` re-exports four `expo-image` statics so call sites never need to import expo-image directly:

```ts
ImageCache.prefetch(uri);              // warm the cache off-screen
await ImageCache.loadAsync({ uri });   // promise-resolving prefetch
ImageCache.clearMemoryCache();         // drop RAM cache only
ImageCache.clearDiskCache();           // drop disk cache (rare)
```

`prefetch` is useful from a list-screen's `onEndReached` when you want to warm images ahead of scroll. Song-list covers rely on per-row `<ImageCache>` instead of catalog-level prefetch. `clearDiskCache` is almost never called from product code — it lives here for diagnostic / debug menus.

## Memory-pressure integration

`ImageCache.clearMemoryCache()` can also be called manually from debug menus. On an OS memory warning, [`releaseMemoryPressure`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/memory/release-memory-pressure.ts) — wired once at the app root by `useAppMemoryBootstrap` — clears it every time; what the same handler does to the audio graph is described in [features/piano.md](/docs/features/piano.md).

The disk cache is intentionally left alone — decoded JPEGs survive a memory warning since the CDN round-trip is more expensive than the local re-decode.

## Ephemeral FTUE screens

Onboarding and tutorial ship large bundled PNG heroes that are not needed after the user completes FTUE. Render them through `<ImageCache cachePolicy="none" />` (not `react-native` `Image`). Metro already serves `require()` assets locally — skipping the expo-image cache lets decoded bitmaps release when the component unmounts, without a separate group-unmount cleanup hook.

Leave remote CDN covers on `cachePolicy="memory-disk"` (e.g. tutorial choose-song thumbnails) — those should survive FTUE and benefit from disk cache.

For tab-persisted screens that must stop work on navigation blur (e.g. song previews), use `useFocusEffect` instead — see [`usePreviewPlayback`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/songs/hooks/usePreviewPlayback.ts).

## When NOT to use ImageCache

- **Small static icons reused app-wide via `require('@assets/...')`.** RN's `Image` is fine when the asset stays in the main app shell indefinitely.
- **UI icons.** Render through the shared [`<Icon>`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/components/Icon.tsx) primitive (tinted PNGs from `@assets/icons/manifest`) — not `ImageCache`. The project has no `react-native-svg` dependency by design; see [.cursor/skills/skia-svg-icons/SKILL.md](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/.cursor/skills/skia-svg-icons/SKILL.md).
- **Layout math on the raw bitmap.** If you need decoded width/height before render (e.g. computing aspect ratio), prefer `Image.getSize` or read it from `onLoad({ source: { width, height } })`.

## Migration checklist

For every remote-image call site:

- [ ] Replace `import { Image } from 'react-native'` with `import { ImageCache } from '@components/ImageCache'`.
- [ ] Rename JSX tag `<Image .../>` → `<ImageCache .../>`.
- [ ] Keep `source`, `style`, `resizeMode`, `onError` unchanged.
- [ ] Add `recyclingKey={item.id}` inside a recycling list's `renderItem` (Legend List `recycleItems`; required: cells are recycled).
- [ ] _(Optional)_ Add `transition={150}` on hero images to mask network latency.

Existing call sites still using `react-native` `Image` (candidates to migrate):

_None right now — the tracks detail screen that used `Image` was removed._

## iOS build: `disableLibdav1d`

[`app.config.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/app.config.ts) configures the `expo-image` config plugin with `disableLibdav1d: true`, which sets `expo-image.disable-libdav1d` in `Podfile.properties.json` on prebuild. That omits the CocoaPods `libavif/libdav1d` dependency (smaller IPA; drops bundled `libavif.framework` weight from the software AV1 decoder). PNG/JPEG/WebP and OS-backed AVIF paths are unchanged. Re-run `npx expo prebuild --clean` (or the next EAS iOS build) for the change to land in the native project.

## See also

- [src/components/ImageCache.tsx](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/components/ImageCache.tsx) — the wrapper source.
- [src/core/memory/](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/memory/) — `ResourceScope`, `useResourceScope`.
- [expo-image documentation](https://docs.expo.dev/versions/latest/sdk/image/) — every renderer prop we don't re-export still works at the underlying level.
- [.cursor/skills/use-image-cache/SKILL.md](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/.cursor/skills/use-image-cache/SKILL.md) — agent skill for adding / migrating call sites.

***

<small>Source: [`docs/images.md`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/docs/images.md)</small>
