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.

Preloading local images used via require() fails

See original GitHub issue

Sometimes with even local images take a short while to load, particularly if there are many images composing full ui i.e. games.

I thought using .preload method on my local images could elevate some bottlenecks, but following errors

export default function preloadImages() {
  FastImage.preload([
    require('../assets/images/dwarf.webp')
])

Whenever I call preloadImages I receive following error

Exception ‘-[__NSCFNumber objectForKeyedSubscript:]: unrecognized selector sent to instance 0xf4263c55c59de1d2’ was thrown while invoking preload on target FastImageView with params ( ( 1 ) )

I assume this number 1 relates to me using require. Should this be supported as part of preload or is this issue irrelevant?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11

github_iconTop GitHub Comments

44reactions
0xAsimetriqcommented, Jan 13, 2019

I found a small hack-around for this by doing following, not sure how I feel about it yet nor what performance implications of this method could be, but we can get uri from require by using react-native’s Image.resolveAssetSource

import { Image } from 'react-native';
import FastImage from 'react-native-fast-image';

export function preloadImages() {
  const images = [
    require('../assets/images/graphic1.webp'),
    require('../assets/images/graphic2.jpg'),
    require('../assets/images/graphic3.webp')
  ];

  const uris = images.map(image => ({
    uri: Image.resolveAssetSource(image).uri
  }));

  FastImage.preload(uris);
}

You can then use <FastImage source={require('../assets/images/graphic1.webp')} /> and image is actually already preloaded. I can see good improvement in my app, memory is not spiking, so I assume it caches to disk not ram?

8reactions
0xAsimetriqcommented, Jan 16, 2019

Solution above works very well for me, only wishing for a callback on preload to know when images have loaded

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to preload images in React.js? - Stack Overflow
First one has nice API easy to understand and use ,but is spaming console with warning that images were not loaded even when...
Read more >
How to Preload Images into Cache in React JS | by Jack P
This function will take in an image array as an argument and will loop through the array with Map. In each iteration, the...
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 responsive images - web.dev
The problem with CSS background images is that they are discovered by the browser only after it has downloaded and processed all the...
Read more >
Preloading and the JavaScript Image() object - TechRepublic
Some browsers try to mitigate this problem by storing the images in the local cache so that subsequent calls to the image are...
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 Reddit Thread

No results found

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