react-native APP Image加载图片

本文介绍了在React-Native应用中加载图片的三种方法:1) 加载网络图片;2) 加载本地资源图片,包括在Android的drawable目录和iOS的相应位置添加图片;3) 加载SD卡中的图片,需要指定file://前缀和权限。同时提供了一个在images.js中管理和在pages中引用图片的例子。
摘要由CSDN通过智能技术生成

react-native APP Image加载图片
*react-native APP Image加载图片共有3中方法:
1.加载外部资源的图片(网络图片)

 <Image style={
   {
   width:100,height:100}} source={
   {
   uri:'https://facebook.github.io/react/img/logo_og.png'}}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
React Native ImagePicker 组件获取到的图片是以 URI 的形式返回的,而不是公共路径。如果您需要将图片保存到公共路径,可以使用 `react-native-fs` 这个第三方库,用于读取和写入文件系统。 以下是一个简单的示例,演示如何将 ImagePicker 返回的图片保存到公共路径: ```javascript import React, { useState } from 'react'; import { Button, Image, View } from 'react-native'; import ImagePicker from 'react-native-image-picker'; import RNFS from 'react-native-fs'; const App = () => { const [image, setImage] = useState(null); const handleChoosePhoto = () => { const options = { mediaType: 'photo', quality: 1, }; ImagePicker.launchImageLibrary(options, response => { if (response.uri) { const folderPath = RNFS.DownloadDirectoryPath; const fileName = 'myImage.jpg'; const filePath = `${folderPath}/${fileName}`; RNFS.copyFile(response.uri, filePath).then(() => { setImage(filePath); }); } }); }; return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> {image && ( <Image source={{ uri: `file://${image}` }} style={{ width: 200, height: 200 }} /> )} <Button title="Choose Photo" onPress={handleChoosePhoto} /> </View> ); }; export default App; ``` 在上面的示例,我们使用 `react-native-fs` 的 `copyFile` 方法将 ImagePicker 返回的图片复制到公共路径,并将公共路径作为 state 更新。最后,我们使用 `file://` 协议将图片加载Image 组件。 希望这能帮到您!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值