Code Monkey home page Code Monkey logo

react-native-blasted-image's Introduction

๐Ÿš€ BlastedImage

npm downloads platform - android platform - ios

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

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. Notably, while it provides these enhanced capabilities for remote images, it seamlessly integrates with the standard React Native Image component when handling local assets.

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.

Installation

Using npm:

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

Using yarn:

yarn add react-native-blasted-image

Link native packages (iOS only)

cd ios && pod install

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. -
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
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. -
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.
-

Methods

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

BlastedImage.preload([
  { uri: 'https://example.com/image1.jpg' },
  { uri: 'https://example.com/image2.jpg', skipMemoryCache: true },
]);
Method PropType Description
BlastedImage.preload() Array<{ uri: string, skipMemoryCache: bool }> 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.

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.

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.

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 ยฉ๏ธ xerdnu
  • SDWebImage - MIT
  • Glide - BSD, part MIT and Apache 2.0. See the LICENSE file for details.

react-native-blasted-image's People

Contributors

sepperousseau avatar toantk911 avatar xerdnu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-native-blasted-image's Issues

Change source if image has error

Hi @xerdnu

I'm handling error by updating a new source but the BlastedImage keeps showing the error icon.

I've created a pull request to fix this. Could you please check?
#4

Types, Sizing

First of all. Good to see someone started on a replacement for react-native-fast-image it looks promising ๐Ÿ‘

I just had a few things that instantly popped up when trying the library by replacing it with the native Image from react-native.

  1. There is currently no typescript support or am I missing something? Might be good to start the library using typescript before it's getting too big.

  2. I saw in the code "if you need width% support log an issue. :D Here I am. Basically a lot of images in our app are sized using styling even without passing width and height. So If I simply swap our Image component all Images are not adapted by the style prop. Currently fixed it by setting the image size based on an onLayout prop inside a wrapper. But that might not be ideal.

  3. Would be nice to have a callback when image is loaded or an extra check function to check if there is an image in the cache. This way we can show a placeholder image if no image is in the cache and fade to the loaded image when done. Or the library could do that as well. But that might be a little much in early stage :)

Happy to hear your thoughts. Keep up the good work ๐Ÿ‘

Loading indicator

Hello, Is there any way how to show custom loading indication while image is loading? I can see OnLoad and OnError callbacks but there is no OnLoadStart

How to use without caching images?

My app has thousands of remote images.
Caching thousands of images will cause the app to shut down due to lack of memory.
Is there a way to quickly import images and just render them without caching them?

[Android]Add APNG support

i add dependencies to build.gradle like react-native-fast-image , but not work.

implementation 'com.github.penfeizhou.android.animation:glide-plugin:2.28.0

Please add Typescript support

My project is completely using typescript and im getting a Typescript Error for the library :)

/node_modules/react-native-blasted-image/index.js' implicitly has an 'any' type.
Try npm i --save-dev @types/react-native-blasted-image if it exists or add a new declaration (.d.ts) file containing declare module 'react-native-blasted-image';ts(7016)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.