无法在React Native中显示多个图像

时间:2019-09-24 14:02:22

标签: javascript reactjs react-native react-native-image

我正在研究React本机应用程序。我必须显示多个图像。我的代码是:

render() {
  if (!this.props.data) {
    return(<Text style={styles.noIMG}>Keine Bilder vorhanden</Text>)
  }
  return (
    <View>
      <View style={styles.view}>
        {
          (
            this.props.data == '' || 
            this.props.data == null || 
            this.props.data == undefined
          ) ? null :
          this.props.data.map(img => {
            console.log(img.image);
            console.log("Image displayed");
            return(
            <TouchableHighlight 
              key={this.uuidv4()} 
              onPress={()=>{
                this.setState({zoom: img.image})
              }}
            >
              <Image 
                style={styles.imagePreview} 
                source={{uri: img.image}} 
              />
            </TouchableHighlight>)
        })
        }
      </View>
      { this.state.zoom ? 
        <Card>
          <PinchZoomView scalable={false}>
            <FitImage resizeMode="contain" source={{uri: this.state.zoom}}/>
          </PinchZoomView>
          <TouchableOpacity 
            style={styles.btn} 
            onPress={() => this.deleteImage(this.state.zoom)}>
            <Text style={{color:'white'}}>Delete Image</Text>
          </TouchableOpacity>
        </Card> : 
        <View style={styles.container}><Text>Bild zum vergrößern antippen.</Text></View>
      }
    </View>
  );
}

我确实在“ props.data”中获得了两个图像,但是在屏幕上仅显示了一个图像。 CSS代码是:

const styles = StyleSheet.create({
    imagePreview:{
    width: 100,
    height: 100,
    margin: 5
    }
});

2 个答案:

答案 0 :(得分:1)

render() {
  return (
    <View style={{ flex: 1, flexDirection:'row'}}>
      {data.map((value, i) => {
        return (
          <View key={i}>
           <TouchableOpacity>
            <Image
              source={{ uri: value.imageUrl }}
              style={{ width: 50, height: 50 }}
              resizeMode={"stretch"}
            />
          </View>
         </TouchableOpacity>
        );
      })}
    </View>
  );
}

答案 1 :(得分:0)

尝试使用FlatList

<FlatList   
    data={this.props.data}
    extraData={this.state}
    renderItem={({ item }) => {
             return (
            <View>
                <TouchableOpacity onPress={() =>  this.setState({zoom: img.image})}>
                    <Image
                        source={{ uri: item.image }}
                        PlaceholderContent={<ActivityIndicator />}                     
                    />
                </TouchableOpacity>

            </View>
        )
    }}
    keyExtractor={(item, index) => index.toString()}
/>