文档地址
我使用的版本为4.0.4
目前最新版本
网上有些案例写法有很大不同,可能跟版本不同有关
npm install react-native-image-picker -s
import React, { useState } from 'react';
import { View, Button, Image } from 'react-native';
import { launchCamera, launchImageLibrary } from 'react-native-image-picker';
const Photo = () => {
const [imgs, setImgs] = useState([])
const addPhoto = () => {
launchImageLibrary({
mediaType: "photo", // 'photo' or 'video' or 'mixed'
selectionLimit: 0,// 1为一张,0不限制数量
includeBase64: true
}, res => {
setImgs(res.assets)
})
}
const addVideo = () => {
launchImageLibrary({
mediaType: "video",
selectionLimit: 1,
}, res => {
console.log(res)
})
}
const tackPhoto = () => {
launchCamera({
mediaType: "photo",
cameraType: "back"
}, res => {
console.log(res)
})
}
return (
<View>
<Button title="启动图库选择图像" onPress={() => addPhoto()}></Button>
<Button title="启动图库选择视频" onPress={() => addVideo()}></Button>
<Button title="启动相机拍摄图片" onPress={() => tackPhoto()}></Button>
{
imgs.map((item, index) => {
return (
<View key={index}>
<Image style={{ width: 50, height: 50 }} source={{ uri: item.uri }}></Image>
</View>
)
})
}
</View>
);
}
export default Photo;
扫码关注腾讯云开发者
领取腾讯云代金券
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. 腾讯云 版权所有