反应本机图像加载太慢

react native image loading is too slow

我从 react-native-image-filter 中获取了 22 张图像。 这张图片是base64格式, 这是渲染运行的代码。

render() {
        const { images, imageIndex } = this.state;
        return (
          <View style={styles.container}>
            {images?.length > 21 && <View style={{ flex: 1 }}>
              <Image resizeMode={'contain'} source={{ uri: `data:image/png;base64,${images[imageIndex]?.base64}` }} style={{ width: 400, flex: 1 }} />
            </View>}
            <View style={{ height: 140, paddingVertical: 20 }}>
              <ScrollView horizontal>
                {images?.map((item, index) => {
                  return (
                    <View style={[{ marginHorizontal: 10 }, styles.center]}>
                      <Image
                        resizeMode={'cover'}
                        key={index.toString()}
                        onPress={() => this.setState({ imageIndex: index })}
                        source={{ uri: `data:image/png;base64,${item.base64}` }}
                        style={{ width: 80, height: 100, borderRadius: 8 }} />
                    </View>
                  )
                })}
              </ScrollView>
            </View>
          </View>
        );
      }

一切都很好,但是图片加载太慢了。 我怎样才能增加加载速度?帮帮我...

问题肯定出在 Base64 编码的图像中。

速度变慢是因为 React Native 必须通过 Native<->JS 桥来回传递相当长的 Base64 字符串,加上原生平台必须解析 Base64 并将其转换为原生图像视图的正确内存表示.因此错过了平台提供的很多优化。

理想情况下,所有图像操作都应该在原生平台上进行,none 的实际图像数据甚至应该接触 JS。

我建议尝试使用这两个库来实现更高性能的过滤器: