这里的Image并不是iOS中的UIImage,而相当于UIImageView组件。通过这个组件可以展示各种各样的图片,而且在React Native中该组件可以通过多种方式加载图片资源。
常见有如下加载方式:
首先先看一下图片的位置:
代码:
render() {
return(
<View style={{marginTop:100}} >
<Image
source={require('./image/image1.jpg')}
style={{width: 120, height: 120}} />
</View>
);
}
效果:
还是先看下目录文件:
代码:
render() {
return(
<View style={{marginTop:100}} >
<Image
source={{uri:'test'}}
style={{width: 120, height: 120}} />
</View>
);
}
效果图:
代码:
render() {
return(
<View style={{marginTop:100,alignItems:'center'}} >
<Image
source={{uri:'https://www.baidu.com/img/bd_logo1.png'}}
style={{width: 120, height: 120}} />
</View>
);
}
效果:
先说下加载Image的回调方法:
<Image
source={{uri:'https://www.baidu.com/img/bd_logo1.png'}}
style={{width: 120, height: 120}}
onLayout={()=>{alert('布局变化回调')}}
onLoad={()=>{console.log('加载回调')}}
onLoadEnd={()=>{console.log('加载结束')}}
onLoadStart={()=>{alert('开始加载')}}
/>
console.log在控制台输出,类似OC nslog
想必大家也发现了加载网络图片的时候,图片被遮挡住了。这是因为图片大小已经超过了我们指定宽高的相框,然而相片默认属性并不是自适应。那我们应该调节resizeMode属性来达到想要的内容。
看下contain是否能达到我们想要的效果:
FlexBox 支持弹性盒子风格 Transforms 支持属性动画 backgroundColor 背景颜色 borderColor 边框颜色 borderWidth 边框宽度 borderRadius 边框圆角 overflow 设置图片尺寸超过容器可以设置显示或者隐藏('visible','hidden') tintColor 颜色设置 opacity 设置不透明度0.0(透明)-1.0(完全不透明) 代码: style={{width: 120, height: 120 ,backgroundColor:'red',borderColor:'green',borderWidth:1,borderRadius:5,tintColor:'yellow',opacity:0.2}} 效果:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有