Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-native-blasted-image

xerdnu4.5kMIT, BSD, Apache-2.01.1.2TypeScript support: included

A simple yet powerful image component for React Native, powered by Glide and SDWebImage

react-native, cached, image, image-cache, fastimage, fast-image

readme

:rocket: BlastedImage

npm downloads npm downloads platform - android platform - ios

A simple yet powerful image component for React Native, powered by Glide (Android) and SDWebImage (iOS).

Support My Work! 🎉

I truly appreciate your support! If you'd like to help me out, the best way is to check out my latest app — LogoDuel.

LogoDuel is a fun, turn-based multiplayer trivia game where you challenge friends (or foes!) to guess famous logos. Test your brand knowledge and see who comes out on top!

🚀 Powered by BlastedImage for performant and optimized image handling.

👉 Download now and let the logo battle begin!

Get it on Google Play Download on the App Store

Description

Caching remote images has always been a challenge for me with the Image component in React Native. This simplified, yet powerful component, addresses that issue head-on. It offers a robust and performant mechanism for caching remote images, ensuring they're displayed quickly.

Leveraging the strengths of Glide and SDWebImage, it supports both memory and disk caching for remote images. The newly added Hybrid Assets feature allows you to bundle remote assets in your build, fetching from the network only when necessary. Notably, while it provides these enhanced capabilities for remote images, it seamlessly integrates with the standard React Native Image component when handling local assets using require.

Features

  • Performance: Bypasses the React Native Image component for remote images ensuring immediate and lightning-fast display of remote images.
  • Cross-Platform: Works on both Android (with Glide) and iOS (with SDWebImage)
  • Customizable: Wrapped within a View for added layout and style customization.
  • Robust Caching: Benefits from both memory and disk caching for maximum performance.
  • Hybrid Assets: Bundle remote assets within your build and only fetch from the network if assets are not included.

Supported Image Types

  Filetype     Android       iOS    
PNG
APNG
JPEG
SVG
GIF
WebP
AVIF
HEIC
ICO

Installation

With bare React Native

Using npm:

npm install react-native-blasted-image --save

Using yarn:

yarn add react-native-blasted-image
cd ios && pod install

With Expo

Using npm:

npx expo install react-native-blasted-image

Usage

Here's a simple example to get you started:

import BlastedImage from 'react-native-blasted-image';

<BlastedImage 
  source={{ uri: 'https://example.com/image.png' }} 
  resizeMode="cover"
  width={200}
  height={200}
  style={{ borderRadius: 10 }}
/>

Paramaters

Parameter Type Description Default
source Object or require (Required) Can be an object containing a uri string for remote images or local images using require.

(Optional) The object containing uri can also contain hybridAssets and cloudUrl . See specific documentation regarding the source parameter below.
-
width Number (Optional) Specifies the width of the image. Overrides width in style 100
height Number (Optional) Specifies the height of the image. Overrides height in style 100
resizeMode String (Optional) Resize the image with one of the options: cover contain center stretch cover
isBackground Boolean (Optional) Makes the image act as a container background similar to the native ImageBackground component false
returnSize Boolean (Optional) Specifies if Size parameters should be returned in onLoad callback. false
retries Number (Optional) Specifies the number of retry attempts if the image fails to load. 3
tintColor String (Optional) Specifies tintColor for the image using hexadecimal/named colors. -
fallbackSource Object (Optional) Object containing a uri string for a custom error image. -
onLoad Function (Optional) Callback function that gets called when the image has loaded succesfully.
Returns Size parameters of the source image if returnSize set to true
-
onError Function (Optional) Callback function that gets called when there was an error loading the image. -
style Object (Optional) Styles to be applied to the image, e.g., {borderRadius:20}.
See View Style Props for all available styles.
### Source Parameter
Parameter Type Description Default
-------------- ------------------- ----------------------------------------------------------------------------------------------------- ---------
uri String (Required) URI string for remote images or local images using require. -
hybridAssets Boolean (Optional) Enables the Hybrid Assets feature to bundle remote assets locally and fetch from the network if not included. false
cloudUrl String (Optional) Leading URL to the remote assets for Hybrid Assets functionality.
(Required if hybridAssets is enabled)
null

Methods

import BlastedImage from 'react-native-blasted-image';

BlastedImage.preload([
  { uri: 'https://example.com/image1.jpg' },
  { uri: 'https://example.com/image2.jpg', skipMemoryCache: true },
  { uri: 'https://example.com/image3.jpg', skipMemoryCache: true, hybridAssets: true, cloudUrl: "https://www.example.com/" }
], 5);

Note: The last parameter in preload is how many times the image should retry. If not specified it defaults to 3.

Method PropType Description
BlastedImage.preload() Array<{ uri: string, skipMemoryCache: bool, hybridAssets: bool, cloudUrl: string }> Preloads remote images from an array of URIs, with the option to preload only to disk.
BlastedImage.clearDiskCache() - Clears the disk cache for all images.
BlastedImage.clearMemoryCache() - Clears the memory cache for all images.
BlastedImage.clearAllCaches() - Clears both disk and memory caches for all images.

Hybrid Assets

The Hybrid Assets feature allows you to bundle remote assets directly into your build ensuring they are available locally while still enabling network fetching when necessary. This approach can significantly reduce bandwidth usage especially if you know in advance which assets will be used when you bundle your app.

To fully utilize the Hybrid Assets feature it's important to follow the same folder structure remotely as you do locally. This makes it easier to update your project with new assets while keeping everything organized and simplify the process of integrating updates.

Automatic Bundling (Expo only)

When using Expo you can take advantage of the automatic asset bundling feature by adding this to your app.json file and running prebuild. The bundling feature will automatically copy and reference hybrid assets from the ./assets/blasted-image/* directory on both iOS and Android platforms following the same folder structure as your remote assets.

app.json

{
  "expo": {
    "plugins": [
      "react-native-blasted-image",
      {
        "assetsPath": "/project-root/assets/blasted-image"
      }
    ]
  }
}

Note: The assetsPath property is optional. By default, it will use the path ./assets/blasted-image

Run

npx expo prebuild

Manual Bundling

If you prefer to manage your hybrid assets manually or if you are using Bare React Native you need to do the following.

Android

  1. Copy your hybrid assets to ./android/app/src/main/assets/blasted-image/

iOS

  1. Copy your hybrid assets to ./ios/Resources/blasted-image/
  2. Reference your hybrid asset folder in Xcode (Resources -> blasted-image) and make sure it is included in the Build Phase.

Hybrid Assets Example Structure

Local files                         # Files included in your build
│
├── assets                          # Inside root directory of your project
│   │
│   ├── blasted-image               # All images in this folder are checked against the matching location on your cloud storage.
│   │
│   ├────── image_or_folder_1     
│   ├────── image_or_folder_2        
│   └────── ...    
└── ...

Remote files                        # Files in the cloud, for example Firebase etc.
│
├── image_or_folder_1               # Available in hybrid assets, not downloaded
├── image_or_folder_2               # Available in hybrid assets, not downloaded
├── image_or_folder_2               # Not available in hybrid assets, download and cache
└── ...

Note: If you use manual bundling your hybrid assets should not be placed inside the root of your project directory but rather in Resources for iOS and assets for Android.

Events

import { NativeEventEmitter, NativeModules } from 'react-native';

const BlastedImageEvents = new NativeEventEmitter(NativeModules.BlastedImage);

useEffect(() => {
  const subscription = BlastedImageEvents.addListener('BlastedEventLoaded', (data) => {
    console.log(data.message);
  });

  return () => {
    subscription.remove();
  };
}, []);
Event Description
BlastedEventLoaded Triggered when remote images are successfully loaded.
BlastedEventClearedMemory Triggered when the memory cache for all images is cleared.
BlastedEventClearedDisk Triggered when the disk cache for all images is cleared.
BlastedEventClearedAll Triggered when both disk and memory caches for all images are cleared.
BlastedEventLog Provides detailed logging information for better debugging.

Credits

This component was created with inspiration from react-native-fast-image that also uses Glide and SDWebImage. But due to its lack of ongoing maintenance i felt the need to develop this new image component to continue providing robust and performant caching functionality.

Support My Work! 🎉

I truly appreciate your support! If you'd like to help me out, the best way is to check out my latest app — LogoDuel.

LogoDuel is a fun, turn-based multiplayer trivia game where you challenge friends (or foes!) to guess famous logos. Test your brand knowledge and see who comes out on top!

🚀 Powered by BlastedImage for performant and optimized image handling.

👉 Download now and let the logo battle begin!

Get it on Google Play Download on the App Store

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue. If you want to contribute code, please open a pull request.

License

  • BlastedImage - MIT :copyright: xerdnu
  • SDWebImage - MIT
  • Glide - BSD, part MIT and Apache 2.0. See the LICENSE file for details.

changelog

1.1.2 (2025-03-09)

Bug Fixes

  • Merge of pull request to fix typo regarding the fallbackSource width. (#36)

1.1.1 (2025-02-16)

Bug Fixes

  • Fixed a bug regarding warning about module initialization on main thread. (#35)

1.1.0 (2025-01-31)

🚨 Breaking Changes (Android Only)

  • Upgraded Glide from 4.12.0 to 4.16.0 which may cause duplicate class conflicts if another package in your project also includes Glide.

If you encounter a build error stating com.bumptech.glide.GeneratedAppGlideModuleImpl is defined multiple times it means another package in your project is also bundling Glide.

To resolve this check the error message for the conflicting package and remove it if not needed.

Alternatively ensure all dependencies use the same Glide version by adding the following to your android/app/build.gradle file:

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.16.0'
}

✨ New Features

  • Added support for returning image dimensions via onLoad in combination by setting the returnSize param. (#32)
  • Added support for SVG images.
  • Added support for AVIF images. (#20)
  • Added support for changing color with tintColor parameter. (#11)

🔥 Improvements

  • Reworked parts of the code and created renderKey that will make sure images that failed in retry operation re-renders. (#31)
  • Reworked other parts of the code for increased performance.

🔄 Changes

  • Updated documentation.
  • Changed repo for Android buildscript.
  • Increased caching limits.

1.0.9 (2025-01-12)

New Features

  • Added retry feature using the retries parameter if image fails to load.

Improvements

  • Reworked the useEffect and implemented useCallback aswell to no re-create the function every time the component re-renders.
  • Reworked the preload function to use allSettled instead of manual array iteration.
  • Changed various parts of the code to increase performance.

Bug Fixes

  • Fixed a bug where the component did not show additional images due to error. (#26)
  • Fixed logging issues.

1.0.8 (2025-01-03)

Improvements

  • Added request cache for promises to reduce network delay and make sure all images only loads once and remove excessive warning when loading huge amount of images at the same time.

1.0.7 (2024-10-22)

Improvements

  • Added support for local assets using file:// path on android.

1.0.6 (2024-09-20)

Bug Fixes

  • Fixed a bug that caused assetsPath parameter not to be used on iOS.

1.0.5 (2024-09-20)

Improvements

  • Added ASSETS_PATH environment variable to support EAS.

1.0.4 (2024-09-20)

Improvements

  • Added assetsPath parameter to be specified in app.json.

Changes

  • Updated documentation.

1.0.3 (2024-08-18)

Bug Fixes

  • Fixed a bug that caused an infinite loading loop when an image failed to load.

1.0.2 (2024-08-16)

Bug Fixes

  • Fixed a bug where images was not sent into view on iOS.

1.0.1 (2024-08-16)

Improvements

  • Reworked the source parameter.
  • Reworked some code to create stability regarding the Hybrid Assets.

Changes

  • Updated TypeScript definitions.
  • Updated documentation.

Bug Fixes

  • Fixed a bug where the component would crash due to incorrect hybridAssets parameter.
  • Fixed a bug where changing the parameter for hyberAssets would not take effect.

1.0.0 (2024-08-14)

New Features

  • Hybrid Assets: Created a new feature called Hybrid Assets that makes it possible to bundle local assets within your build and only fetch from the network if assets are not included. This helps save bandwidth by bundling assets you know is gonna be used in your current version. To enable this just pass hybridAssets as true and provide a cloudUrl. If using Firebase, the component will automatically handle %2F replacements and trailing ?alt=media removal in the URL. Refer to the documentation for more details.
  • Automatic Hybrid Assets Bundling: Implemented a function for automatic copying and bundling of hybrid assets to iOS and Android that also includes automatic reference creation in Xcode (Expo Only).
  • TypeScript Support: Added TypeScript support (#15, #10)

Improvements

  • Reworked the logic for how images are loaded and displayed
  • Reworked major parts of the component to include support for hybrid remote assets.
  • Reworked various parts of the components code that improves readability and fixes several bugs.
  • Maximum disk caching size is now static and set to 1GB.
  • Maximum memory caching size is now static and set to 100MB.
  • Fixed maximum lifetime of disk caching on iOS that ensures no maximum lifetime.

Changes

  • Changed return informational errors for better debugging.
  • Updated the logging events for better visibility.
  • Removed the ability to pass source headers due to stability concerns.
  • Updated documentation.

Bug Fixes

  • Fixed a bug where the component would crash due to a destroyed activity. (#13)
  • Resolved a warning related to defaultProps deprecation and replaced them with javascript default parameters. (#18, #16)
  • Fixed a bug where no resolve occurred when an empty array was sent in preload. (#15, #8)

0.0.13 (2023-12-19)

Improvements

  • Added proper initialization value for onLoad callback. (#4)

0.0.12 (2023-10-31)

Improvements

  • Added proper onLoad and onError callbacks. (#3)
  • Added fallbackSource prop when image could not load. (#3)

Changes

  • Included static fallback image in cases where both the primary source and the fallbackSource fail to load.
  • Updated documentation.

0.0.11 (2023-10-31)

Changes

  • Changed package name.
  • Minor changes in JS code regarding logging.

0.0.10 (2023-10-24)

Improvements

  • Added the prop isBackground to act as a container background similar to the native ImageBackground component.

Bug Fixes

  • Fixed a bug that caused the error image not to display.

Changes

  • Updated documentation.

0.0.9 (2023-10-19)

Improvements

  • Added the possiblity to pass both single objects and arrays to BlastedImage.preload.
  • Added promise for BlastedImage.preload to indicate when the image has been processed.

Changes

  • Updated documentation.

0.0.8 (2023-10-17)

Improvements

  • Added the skipMemoryCache preload prop to preload images only to disk, keeping memory free.
  • Added return logs within the bridge.
  • Made a few minor fixes in the Android code.

Changes

  • Updated documentation.

0.0.7 (2023-10-16)

Changes

  • Updated documentation.

0.0.6 (2023-10-16)

Improvements

  • Added support for local images using require

Changes

  • Updated documentation.

0.0.5 (2023-10-16)

Bug Fixes

  • Fixed warning regarding event listeners on Android.

Changes

  • Updated documentation.

0.0.4 (2023-10-16)

Bug Fixes

  • Fixed peerDeps in package.json.
  • Fixed licensing details.

0.0.3 (2023-10-16)

Improvements

  • Added support for passing an array of styles.
  • Removed misc that was earlier used for debug.
  • Added image placeholder when it can not be loaded from uri.

Changes

  • Changed default of resizeMode from contain to cover.

0.0.2 (2023-10-16)

Added

  • Initial release (sync with npm).

0.0.1 (2023-10-15)

Added

  • Initial release.