react-native-swiper 实现绑定接口动态获取轮播图路径 图片不显示的原因

问题描述:

后端提供了如下面示例的图片路径:

fileUrl: https://xxxx.xxxxxx.com.cn/api/partyApp/banner/showImage?fileId=95cc2b1557644161adb92022fef75dde // 是二进制,非.png也非base64的图片 

后端提供的接口数据数组中,图片资源的字段是fileUrl,直接在浏览器上可访问,但是放到react-native的Image组件,就不起效了,整个swiper组件一片空白,为什么?(接口数据取到了,但是图片资源路径无法在程序中的Image组件显示,但是直接浏览器粘贴地址可以看到图片)

问题分析: 

原因是,我们的后端设置了token验证,需要在图片资源source的uri字符串末尾加上&auth.token=${Zqmb.businessToken} // 这边变量自行替换

处理前的代码:

{bannerList.map((banner, index) => {
              return (
                // <ImageWrapper icon={'\uf652'} size={26} color="#666" />
                <Image
                  key={index}
                  style={{
                    flex: 1,
                    justifyContent: 'center',
                    alignItems: 'center'
                  }}
                  source={{ uri: banner.fileUrl }}
                  style={styles.img}
                />
              );
            })}

处理后的代码: 

{bannerList.map((banner, index) => {
              return (
                // <ImageWrapper icon={'\uf652'} size={26} color="#666" />
                <Image
                  key={index}
                  style={{
                    flex: 1,
                    justifyContent: 'center',
                    alignItems: 'center'
                  }}
                  source={{ uri: `${banner.fileUrl}&auth.token=${Zqmb.businessToken}` }}
                  style={styles.img}
                />
              );
            })}

完整代码: 

{bannerList.length ? (
          <Swiper
            loop={true} //如果设置为false,那么滑动到最后一张时,再次滑动将不会滑到第一张图片。
            autoplay={true} //自动轮播
            autoplayTimeout={1} //每隔4秒切换
            horizontal={true} //水平方向,为false可设置为竖直方向
            // paginationStyle={{ bottom: 0 }} //小圆点的位置:距离底部10px
            showsButtons={false} //为false时不显示控制按钮
            showsPagination={true} //为false不显示下方圆点
            // top={NAVBAR_HEIGHT}
            dot={
              <View
                style={{
                  //未选中的圆点样式
                  backgroundColor: OAColor.swiperDot,
                  width: OASize(5),
                  height: OASize(5),
                  borderRadius: OASize(2.5),
                  marginHorizontal: OASize(5)
                }}
              />
            }
            activeDot={
              <View
                style={{
                  //选中的圆点样式
                  backgroundColor: OAColor.activeDot,
                  width: OASize(5),
                  height: OASize(5),
                  borderRadius: OASize(2.5),
                  marginHorizontal: OASize(5)
                }}
              />
            }
          >
            {bannerList.map((banner, index) => {
              return (
                // <ImageWrapper icon={'\uf652'} size={26} color="#666" />
                <Image
                  key={index}
                  style={{
                    flex: 1,
                    justifyContent: 'center',
                    alignItems: 'center'
                  }}
                  source={{ uri: `${banner.fileUrl}&auth.token=${Zqmb.businessToken}` }}
                  style={styles.img}
                />
              );
            })}
          </Swiper>
        ) : null}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hzxOnlineOk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值