【问题标题】:Why Image.resolveAssetSource() is not returning the height and width in react native?为什么 Image.resolveAssetSource() 不返回反应原生的高度和宽度?
【发布时间】:2019-04-01 09:39:59
【问题描述】:

我最近搬到 react native 并想获得 uri 图像的大小。我查看了react nativedocumentation,发现this方法:

Image.resolveAssetSource();

当我将它用于 static images(require()) 时,它可以正常工作并返回 height 和 width 属性以及其他有用的属性,但是当我将它用于 动态图像时({uri: 'www.abc.com/image'}) 它仅返回对象中包含的 uri 属性,但不返回 height 和 宽度属性。

我在想可能是我做错了什么,但实际上documentation 提到:

ImageSource 是一个类似于 { uri: 'http location || 的对象文件路径'}

那么为什么它不适用于 uri?谁能告诉我?

【问题讨论】:

  • Image.getSize(myUri, (width, height) => {this.setState({width, height})});

标签: android ios reactjs react-native ecmascript-6


【解决方案1】:

【讨论】:

    【解决方案2】:

    要获取远程图像尺寸,请使用Image.getSize(uri, success, failure)

    使用示例:

    Image.getSize(
        'https://link-to-image',
        (width, height) => {
            console.log(`Width: ${width}, Height: ${height}`);
        },
        (error) => console.error(error)
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多