question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preload local images

See original GitHub issue

Is there anyway to preload local images that are required()?

I’m using the react-native-navigation module for screen management and when the view loads the image is not visible, then after the animation completes, it renders the image.

I have the following render() function:

render() {
    let styles = {
        position:        'absolute',
        top:             0,
        left:            0,
        flex:            1,
        width:           Dimensions.get('screen').width,
        height:          Dimensions.get('screen').height,
    };

    return (
        <ScrollView>
            <FastImage
                source={require('./assets/images/background.png')}
                style={styles.bgImage}
                resizeMode={'cover'} />
        </ScrollView>
    );
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

16reactions
camdagr8commented, Jul 3, 2018

@koodiohto: If it helps, I figured out a way to cache local images. I run the following command when my app is setting up:

List of images to preload:

    // Preload images with require();
    const images = {
      'icon.png': require('./icon.png'),
      'orbs-1.png': require('./orbs-1.png'),
      'orbs-2.png': require('./orbs-2.png'),
      'orbs-3.png': require('./orbs-3.png'),
      'survey-1.png': require('./survey-1.png'),
    };

Image processor:

    // Convert image refs into image objects with Image.resolveAssetSource
    loadImages(images) {        
        return Promise.all(Object.keys(images).map((i) => {
            let img = {
                ...Image.resolveAssetSource(images[i]),
                cache: 'force-cache'
            };

            return Image.prefetch(img);
        }));
    }

Usage:

 loadImages(images).then((results) => { /* do something with the image objects */ });
11reactions
DylanVanncommented, May 4, 2018

I am going to add support for local images. Probably soon / in the next major version. For now it does fallback to Image though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to preload local images? - javascript - Stack Overflow
The best option would be if you preload them in componentDidMount or useEffect hook. import avatar from 'assets/avatar.png' import plants ...
Read more >
Preloading responsive images - web.dev
Learn about new and exciting possibilities for preloading responsive images to ensure great user experience.
Read more >
Preloading local images used via require() fails #395 - GitHub
Sometimes with even local images take a short while to load, particularly if there are many images composing full ui i.e. games.
Read more >
3 Ways to Preload Images with CSS, JavaScript, or Ajax
Three easy ways to preload images using either CSS, JavaScript, or Ajax - an excellent way to speed things up and boost site...
Read more >
Preloading Images | kirupa.com
What you need to do is load any images that will be used dynamically into your browser's cache. This is known as preloading....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found