简体   繁体   English

反应原生图像裁剪选择器在平面列表上显示(渲染)图像时出现问题

[英]Having issues with react native image crop picker to display (render) images on flatlist

So i got this component to pick or select multiple images and save it (or not) in this empty array to display them in a sort of carousel, at least this my idea, anyways, this is my function所以我让这个组件选择或 select 多个图像并将其保存(或不保存)在这个空数组中以将它们显示在一种轮播中,至少这是我的想法,无论如何,这是我的 function

   const selectImage = async () => {
    const ImageList = [];
    ImagePicker.openPicker({
      multiple: true,
      waitAnimationEnd: false,
      includeExif: false,
      forceJpg: true,
      compressImageQuality: 0.3,
      maxFiles: 10,
      mediaType: 'photo',
      includeBase64: true,
    }).then(response => {
        response.map(image => {
          setImages(ImageList.push({
            id: ImageList.length,
            filename: image.filename || Moment().toString(),
            path: image.path,
            type: image.mime,
            data: image.data,
          }));
        });
        setImages(ImageList);
        console.log(ImageList);
      }).catch((e) => {
        console.log('ERROR ' + e.message);
        Alert.alert('ERROR ', e.message);
      });
  };

the result of the object is this object 的结果是这样的

在此处输入图像描述

so a simple way to show the images is this所以显示图像的一种简单方法是

            Object.keys(Images).length !== 0 ? Images && (
              Images.map((value, index) => {
                return (
                  <View key={index.key}>
                    <Text>{value.filename}</Text>
                    <Image style={{width: 200, height: 200}} source={{uri: `data:${value.type};base64,${value.data}`}}/>
                  </View>
                );
              })
            )
: null

so that works flawless, but this doesn't...所以它完美无瑕,但这并不......

      <FlatList
        data={Images}
        horizontal={true}
        decelerationRate="fast"
        bounces={false}
        renderItem={(item, index) => {
          <View style={styles.alignView}>
            <View style={styles.titleTextView}>
              <Text>{item.filename}</Text>
              <Image style={{width: 200, height: 200}} source={{uri: `data:${item.type};base64,${item.data}`}}/>
            </View>
          </View>;
        }}
        keyExtractor={(item, index) => item.id}
      />

at first i thought it was the key extractor, but it doesn't seem to be the problem起初我以为是钥匙提取器,但似乎不是问题

EDIT: Solved, i was missing a {} inside the render item plus a return编辑:已解决,我在渲染项中缺少 {} 以及返回

                renderItem={({item, index}) => {
              return                   <View style={styles.alignView}>
              <View style={styles.titleTextView}>
                <Text>{item.filename}</Text>
               <Image style={{width: 200, height: 200}} source={{uri: `data:${item.type};base64,${item.data}`}}/>
              </View>
            </View> //<Text>{item.filename}</Text>; /**/
            }

I'd suggest you to optimise selectImage我建议你优化selectImage

const selectImage = async () => {
    await ImagePicker.openPicker({
      multiple: true,
      waitAnimationEnd: false,
      includeExif: false,
      forceJpg: true,
      compressImageQuality: 0.3,
      maxFiles: 10,
      mediaType: 'photo',
      includeBase64: true,
    }).then(response => {
        const ImageList = response.map((image, index) => ({
            id: index,
            filename: image.filename || Moment().toString(),
            path: image.path,
            type: image.mime,
            data: image.data,
        }));
        setImages(ImageList);
        console.log(ImageList);
      }).catch((e) => {
        console.log('ERROR ' + e.message);
        Alert.alert('ERROR ', e.message);
      });
  };

and your renderItem function returns undefined.并且您的renderItem function 返回未定义。

This is correct version这是正确的版本

<FlatList
    data={Images}
    horizontal={true}
    decelerationRate="fast"
    bounces={false}
    renderItem={(item, index) => (
      <View style={styles.alignView}>
        <View style={styles.titleTextView}>
          <Text>{item.filename}</Text>
          <Image style={{width: 200, height: 200}} source={{uri: `data:${item.type};base64,${item.data}`}}/>
        </View>
      </View>
    )}
    keyExtractor={(item, index) => item.id}
/>

use parentheses instead of curly braces after =>=>之后使用括号而不是花括号

                  renderItem={({item, index}) => {
                  return (
                    <View style={styles.alignView}>
                      <View style={styles.titleTextView}>
                        <Text>{item.filename}</Text>
                        <Image style={{width: 200, height: 200}} source={{uri: `data:${item.type};base64,${item.data}`}}/>
                      </View>
                    </View>
                  );                   
                  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM