【问题标题】:Is it possible to using Image.getSize with static image file?是否可以将 Image.getSize 与静态图像文件一起使用?
【发布时间】:2017-06-19 06:03:44
【问题描述】:

我想使用 Image.getSize (https://facebook.github.io/react-native/docs/image.html) 来获取图像的大小,但第一个参数要求图像源在 URI 中,但我不能将 URI 与静态文件一起使用,我只能使用 Require .

因此,是否可以在静态文件上使用 Image.getSize 或者我必须找到另一种方法?

【问题讨论】:

    标签: image react-native static-files


    【解决方案1】:

    通过official document

    import { Image } from 'react-native'
    
    const url = require('./x.png')
    const image = Image.resolveAssetSource(url)
    

    然后使用image.widthimage.height

    【讨论】:

      【解决方案2】:
      let path = "file:///storage/emulated/0/Pictures/xxx.jpg";
      Image.getSize(path,(w,h)=>{
              console.log('w h=',w,h);
          },(err)=>{
              console.log('err....',err);
          })
      

      【讨论】:

      • 这是个好主意吗?我不认为这应该只适用于andorid而不是ios。
      • Image.getSize 方法只能获取远程图像尺寸,不能获取静态/本地图像尺寸。参考link
      【解决方案3】:

      您可以使用Image.resolveAssetSource(source).widthImage.resolveAssetSource(source).height

      React Native 文档中的参考:https://facebook.github.io/react-native/docs/image.html#resolveassetsource

      【讨论】:

        【解决方案4】:

        您可以使用 resolveAssetSource(来自 react-native/Libraries/Image/resolveAssetSource)来获取图像的大小。

        import resolveAssetSource from 'resolveAssetSource';
        

        let icon =  require('./assets/images/icon.png'); 
        let source = resolveAssetSource(icon);
        // source.width, source.height`
        

        【讨论】:

        • 嗨!我编辑了您的代码部分并将它们放入代码块中。您可以使用编辑器中的{} 按钮自己执行此操作。您可以在此处添加对您提到的包的引用吗?这将是一个很好的答案。
        • @Pekka웃,我可能错了,但我使用了 require :const resolveAssetSource = require('resolveAssetSource');
        • 这还能用吗,如何将它导入到普通的 react-native 应用程序中?
        • 成功import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
        • 我试图在我的代码中使用 resolveAssetSource,但我失败了。以下包括我在做什么的一个例子:github.com/DylanVann/react-native-fast-image/issues/…你能解释一下我做错了什么吗?
        猜你喜欢
        • 1970-01-01
        • 2018-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多