欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[React Native]map遍历以及获取图片尺寸 博客分类: React Native 遍历图片尺寸 

程序员文章站 2024-03-23 11:57:34
...

1.array里面装了图片

state = {
        array: [image_1, image_2, image_3, image_4, image_5,image_kc1,image_kc2,image_kc3,image_kc4,image_kc5],
      }

 2.遍历这些图片:

renderChildren() {
        return this.state.array.map((i, key) => {
              return (
                <View style={this.getChildrenStyle(i)} key={key}>
                <Text>{key}</Text>
                  <Image source={i}/>
                </View>
              ); 
            }, this);
      }

上面使用了map遍历,key是index,i是value. key={key}不可或缺,否则报错.

 

获取图片尺寸:

import resolveAssetSource from 'resolveAssetSource';

getChildrenStyle(i) {
        return {
          width: resolveAssetSource(i).width,
          height: resolveAssetSource(i).height,
        };
      }

通过上面的遍历,传入的i,可以获得item,然后获取高度和宽度 

相关标签: 遍历 图片尺寸

上一篇: eclipse关联JDK源码图文

下一篇: