Reputation: 625
The setup!
I have dynamic loading images in a chat - like interface with messages that are just text, or messages that are images and text. Currently I have it working where it loading images from an external url that is stored in firebase storage with the meta data CacheControl set to allow image cache.
I am displaying the image through a standard react-native library Image in a pretty standard flatlist. The one thing that I am doing that is weird is conditionally adding a component based on the state in the flatlist data. EXAMPLE:
ImageOrViewMessages = (props) => {
if(item != null && item.image != ''){
return (
<View>
<Image source={{uri: item.image, cache: "force-cache"}} style={{display: 'flex', height: 300, flex: 1, padding: 5}} />
</View>
)
} else {
return(
<View style={{display: 'none'}}></View>
)
}
}
Here is my flatlist:
<FlatList
data={this.props.unsent_messages.length > 0 && this.props.chat != '' ? this.props.unsent_messages.concat(this.props.messages) : this.props.messages}
renderItem={({item}) => {
var align_text = "flex-end"
var background_color = colors.main_color_light;
var text_color = "white"
var stamp = "none"
if(item.username.toLowerCase() != this.props.displayName.toLowerCase()){
//do something differ
align_text = "flex-start"
background_color = colors.main_color_dark;
text_color = "white"
stamp = "flex"
}
ImageOrViewMessages = (props) => {
if(item != null && item.image != ''){
return (
<View>
<Image source={{uri: item.image, cache: "force-cache"}} style={{display: 'flex', height: 300, flex: 1, padding: 5}} />
</View>
)
} else {
return(
<View style={{display: 'none'}}></View>
)
}
}
return(
<View style={{padding: 5, minHeight: 70, alignItems: align_text, justifyContent: "center"}}>
<View style={{opacity: item.id.toString().includes("Unset") ? .3 : 1, backgroundColor: background_color, padding: 5, borderWidth: 0, borderColor: colors.border_color,borderRadius: 8 , width: (Dimensions.get('window').width / 2) - 10, alignSelf: align_text}}>
<Text style={{fontFamily: "MarkerFelt-Thin", color: text_color, fontSize: 16, textAlign: "left", flex: 1}}>{item.message}</Text>
{/* <Text style={{display: stamp, fontFamily: "MarkerFelt-Thin", color: colors.background_color, fontSize: 14, textAlign: "right", flex: 1}}>{item.username}</Text> */}
<ImageOrViewMessages />
</View>
</View>
)}
}
keyExtractor={item => item.image != '' ? item.image : item.id.toString()}
style={{display: 'flex', flex: 1, borderTopLeftRadius: 25, borderTopRightRadius: 25}}
ref={ref => this.flatList = ref}
inverted={true}
/>
The Issue!
The images render perfectly fine, but every single time they are having to download from my server, rather than storing in cache. The things I have tried are making sure my metadeta on the uploaded image is set correctly. Forcing the cache through the Image property "Cache". I have made sure I have a unique key set for each item in the list including images through the key extractor. I am also able to cache the image on my main page without any issue (when it is no longer in a flatlist)
Upvotes: 2
Views: 4806
Reputation: 5186
Yeah, it will be re-render image from server everytime. You need to cache image in default application or device memory. Best way is the FastImage for lazyloading.
Replace your image load with below one:
<FastImage
style={styles.image}
source={{
uri: YOUR_IMAGE_URL,
priority: FastImage.priority.normal,
}}
resizeMode={FastImage.resizeMode.contain}
/>
Don't forgot to import FastImage.
import FastImage from 'react-native-fast-image'
Upvotes: 3