【问题标题】:React Native how replace image url on error?React Native 如何在错误时替换图像 url?
【发布时间】:2019-04-17 07:24:03
【问题描述】:

我有一个应用程序,它显示了我从 API 加载的多个图像。现在的问题是某些图像已过期,这导致 Android 出现问题,在 Android 中,一旦过期图像加载到屏幕上,屏幕就会开始滞后。

我试过用onError={() => this.imgRefs[img_unique_id].setNativeProps({src: [{uri: this.state.workingUri}]})}这个方法替换图像源,但它不起作用。 我不能使用本地状态,因为它没有将输出保存在本地状态。

我试过下面的代码

  <Image  
      source={image.url} 
      progressiveRenderingEnabled={true}
      ref={item.id} 
      onError={(e) => this.refs[item.id].setNativeProps({source: [{uri: "working image URL"}]})} 
      resizeMethod={"scale"}>
   </Image>      

上面的代码给了我一个未定义的 setNativeProps 错误,如果我不在 android 上使用 onError,它会显示内存泄漏错误。

【问题讨论】:

  • 这种情况下为什么不能使用本地状态?
  • 只需使用 native-base 而不是原始的 react-native 构造,native-base Image 通过为您提供支持 fallbackSource 甚至 fallbackComponent 来支持失败的图像

标签: react-native react-native-android react-native-image


【解决方案1】:

这是一个完整的例子。为了让每个 FlatList 项目都有自己的状态,我创建了一个类。

import React, { Component, PureComponent } from 'react';
import { FlatList, Text, View, Image } from 'react-native';

class ItemClass extends PureComponent {
  state = {
    isImageExit: null
  }
  componentWillMount = () => {
    const { task } = this.props;
    Image.getSize(task.url, (width, height) => {
        if(width>0){
            this.setState({ isImageExit: true });
        }
        else{
            this.setState({ isImageExit: false });
        }
    }, () => {
      this.setState({ isImageExit: false });
    });
  }

  render() {
    const { isImageExit } = this.state;
    const { task } = this.props;
    const url = isImageExit ? task.url : 'https://dummyimage.com/600x400/000/fff';
    if (isImageExit === null) {
      return null;
    }
    return (
      <Image
        style={{ height: 200, width: 200 }}
        source={{ uri: url }}
      />
    );
  }
}

export default class App extends Component {
  render() {
    const data = [
      { url: 'url' },
      { url:'https://cdn.pixabay.com/photo/2017/08/05/18/53/mountain-2585069_1280.jpg' },
    ];
    return (
      <View style={{alignItems: 'center', top: 50}}>
          <FlatList
            data={data}
            renderItem={({ item }) => <ItemClass task={item} />}
          />
      </View>
    );
  }
}

【讨论】:

  • 您的代码运行良好,但一旦错误图像进入视图应用程序变慢,问题仍然相同。
  • 如何使用 getSize 预加载图像,而不是等待图像进入错误状态?
  • 已更新,也许更好?首先 image.getSize 将尝试下载并获取图像的大小。在此工作期间,不会渲染任何内容,或者您​​可以渲染加载组件或您想要的内容。 getSize 完成后,图像也会渲染。
  • 我实现了更改它仍然很慢它认为图像在加载更多后使 FlatList 变慢。我试过没有图像它工作正常。
【解决方案2】:

我认为您应该使用从 Api 接收到的数据并在 componentWillReceiveProps 中相应地设置状态,因为我认为设置状态是实现此结果的最佳方式。 .

 this.setState = { image: { uri: 'image_uri_from_api' } }

&lt;Image&gt;里面添加-

 <Image style={ your_custom_style }
       source={ this.state.image }
       onError={ this.onError.bind(this) }
 />

onError里面添加这个-

onError(error){
   this.setState({ image: require('your_local_image.path')})
 }

希望它现在可以工作!

【讨论】:

  • 这是 react native 不是 react web
  • 感谢您让我知道。我已经相应地更新了答案。
  • 动态加载多张图片,如何处理?
  • 是的,我正在使用平面列表。在 iOS 上,它在 Android 上的操作系统端处理它会造成内存泄漏。
  • 有没有一种方法可以在源代码中传递一个函数,该函数首先获取图像并根据大小返回图像或 your_local_image ?。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 2016-04-26
相关资源
最近更新 更多