react-native-fast-image

repository·main·Indexed 27 days ago

https://github.com/dylanvann/react-native-fast-image

A performant React Native image component (version 8.6.3) that wraps SDWebImage on iOS and Glide on Android. It provides aggressive caching, priority loading, and header support to resolve flickering and cache misses. Features include customizable cache control (immutable, web, cacheOnly), resize modes, image preloading, and methods to clear memory and disk caches.

Tokens
2.8K
Snippets
9
Records
21
Agent score
88%

What's inside react-native-fast-image

  1. Configure Proguard for Android

    main

    If you use Proguard, you must add the following rules to your android/app/proguard-rules.pro file to prevent the library and its dependencies (Glide) from being stripped during the build process.

    -keep public class com.dylanvann.fastimage.* {*;}
    -keep public class com.dylanvann.fastimage.** {*;}
    -keep public class * implements com.bumptech.glide.module.GlideModule
    -keep public class * extends com.bumptech.glide.module.AppGlideModule
    -keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
      **[] $VALUES;
      public *;
    }
  2. Prevent AppGlideModule conflicts in Android

    main

    If your Android application already uses a custom AppGlideModule (via Glide), you must prevent react-native-fast-image from including its own AppGlideModule to avoid build conflicts.

    To do this, add the excludeAppGlideModule flag to your android/build.gradle file within the project.ext block.

    project.ext {
        excludeAppGlideModule = true
    }
  3. Understand the image caching strategy

    main

    The library treats image URLs as immutable. It assumes that the data located at a specific URL will not change.

    To handle updates to content that shares a URL (like a user profile picture), you should ensure that the backend provides a new, unique URL when the content changes. This allows the library to fetch the new image instead of serving the old cached version associated with the previous URL.

  4. Configure Android SDK and Glide versions for non-clean React Native installs

    main

    If you are not using a clean React Native installation and have defined project-wide properties in your root build.gradle, react-native-fast-image will attempt to detect and use those properties. You can override the library's default versions for the Android SDK and Glide by setting the following keys in your root build.gradle's ext block:

    /**
     + Project-wide Gradle configuration properties
     */
    ext {
        // You can use any of these to change project wide versions:
        // compileSdkVersion   = 26
        // targetSdkVersion    = 26
        // minSdkVersion       = 16
        // buildToolsVersion   = "26.0.3"
        // supportLibVersion   = "27.1.1"
        // glideVersion        = "4.7.1"
    }
  5. Run the example app for development

    main

    To test changes or run the example application, follow these steps in order. Note that this process uses a modified CLI to handle symlinked packages. You must re-run the iOS or Android run commands whenever you re-compile native code.

    # In the repo root folder.
    # Install dependencies.
    yarn
    
    # Link module.
    yarn link
    
    # Move to example folder.
    cd ReactNativeFastImageExample
    
    # Install dependencies.
    yarn
    
    # Link module.
    yarn link react-native-fast-image
    
    # Start packager.
    yarn start
    
    # Start the iOS app.
    yarn react-native run-ios
    
    # Start the android app.
    yarn react-native run-android
  6. Resolve common build and dependency issues

    main

    If you encounter issues with react-native-fast-image during development or build processes, follow these troubleshooting steps to clear caches and reset environments:

    Environment & Dependency Resets

    • Remove and reinstall node modules: rm -rf node_modules && yarn (or npm install)
    • Clear Watchman watches: watchman watch-del-all
    • Clear React Native packager cache: react-native start --reset-cache

    iOS Specific Troubleshooting

    • Update CocoaPods repositories: cd ios && pod repo update
    • Reinstall Pods: cd ios && pod install
    • Clear iOS build folder: rm -rf ios/build
    • Xcode maintenance: Run 'Clean' in Xcode or delete Xcode's derived data.
    # Common sequence to reset environment
    rm -rf node_modules && yarn
    watchman watch-del-all
    cd ios && pod install && cd ..
    react-native start --reset-cache
  7. Use FastImage component

    main

    Import FastImage from react-native-fast-image to replace the standard React Native Image component. It supports advanced features like authorization headers, image priority, and custom cache control.

    import FastImage from 'react-native-fast-image'
    
    const YourImage = () => (
        <FastImage
            style={{ width: 200, height: 200 }}
            source={{
                uri: 'https://unsplash.it/400/400?image=1',
                headers: { Authorization: 'someAuthToken' },
                priority: FastImage.priority.normal,
            }}
            resizeMode={FastImage.resizeMode.contain}
        />
    )
  8. Configure resizeMode

    main

    The resizeMode prop determines how the image is scaled within its container using FastImage.resizeMode:

    • FastImage.resizeMode.contain: Scales uniformly to maintain aspect ratio; image fits within dimensions.
    • FastImage.resizeMode.cover (Default): Scales uniformly to maintain aspect ratio; image fills dimensions.
    • FastImage.resizeMode.stretch: Scales width and height independently (may change aspect ratio).
    • FastImage.resizeMode.center: Keeps the image centered without scaling.
  9. Preload images with FastImage.preload

    main

    Use FastImage.preload to load images into the cache before they are actually rendered in the UI.

    FastImage.preload([
        {
            uri: 'https://facebook.github.io/react/img/logo_og.png',
            headers: { Authorization: 'someAuthToken' },
        },
        {
            uri: 'https://facebook.github.io/react/img/logo_og.png',
            headers: { Authorization: 'someAuthToken' },
        },
    ])
  10. Clear image caches

    main

    You can programmatically clear the image caches using the following static methods:

    • FastImage.clearMemoryCache(): Returns a Promise that resolves when the memory cache is cleared.
    • FastImage.clearDiskCache(): Returns a Promise that resolves when the disk cache is cleared.