- SDK Version: 35
- Platforms: iOS
I’m having trouble successfully caching and rendering static images. I cache my static (external) assets according to the docs, but when I use the cached image in an ImageBackground
, the image flickers as if it’s being downloaded for the first time. (With some files, it seems like the image is not being cached at all because if I pass in cache: 'only-if-cached'
, the image doesn’t load. In case it’s relevant, I’m using Firebase Storage to store the external images in my actual app.)
Preloading the assets…
function cacheImages(images) {
return images.map(image => {
if (typeof image === 'string') {
return Image.prefetch(image);
} else {
return Asset.fromModule(image).downloadAsync();
}
});
}
function cacheFonts(fonts) {
return fonts.map(font => Font.loadAsync(font));
}
export default class AppContainer extends React.Component {
state = {
isReady: false,
};
async _loadAssetsAsync() {
const imageAssets = cacheImages([
'https://via.placeholder.com/3000/09f/fff.png',
]);
const fontAssets = cacheFonts([FontAwesome.font]);
await Promise.all([...imageAssets, ...fontAssets]);
}
render() {
if (!this.state.isReady) {
return (
<AppLoading
startAsync={this._loadAssetsAsync}
onFinish={() => this.setState({ isReady: true })}
onError={console.warn}
/>
);
}
return (
<View>
<AppContainer />
</View>
);
}
}
And then using the cached image…
export default class AppContainer extends Component {
render() {
return (
<ImageBackground
source={{
uri: 'https://via.placeholder.com/3000/09f/fff.png',
// cache: 'only-if-cached',
}}
imageStyle={{resizeMode: 'cover'}}
style={{flex: 1}}
>
<View>
...stuff
</View>
</ImageBackground>
)
}
}
What am I doing incorrectly or not understanding?