Architecture Overview
@dynlabs/react-native-image-to-webp uses React Native's New Architecture (TurboModules) to provide high-performance image conversion.
๐๏ธ Core Layersโ
1. JavaScript / TypeScript (src/)โ
- API: Exposes
convertImageToWebPand theuseImageConverterhook. - Validation: Ensures inputs are valid before crossing the bridge.
- Presets: Maps human-friendly names (e.g.,
balanced) to technical WebP settings.
2. TurboModule Bridgeโ
- Uses Codegen for type-safe synchronous communication between JS and Native.
- Passes RGBA buffers and conversion parameters to native threads.
3. Native Layer (ios/, android/)โ
- Processes all work on background threads to keep the UI responsive.
- Decoders: Uses platform-native tools (
ImageIOon iOS,ImageDecoderon Android). - Processing: Handles resizing (preserving aspect ratio) and EXIF orientation.
4. C++ Encoding (cpp/)โ
- Shared between iOS and Android.
- Wrapper around libwebp library.
- Efficiently encodes raw pixel data into a WebP file.
๐งต Threading Modelโ
Zero work is done on the main thread:
- JS initializes the call.
- Bridge hand-offs to a dedicated background worker.
- Native code decodes and encodes.
- Bridge returns the result to JS.
๐ Battery & Memoryโ
By resizing before encoding (using maxLongEdge), the library minimizes peak memory usage and CPU cycles, ensuring minimal impact on device resources.