[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源码图文