安装插件
npm install react-native-image-picker
// 引入
import { launchCamera, launchImageLibrary} from 'react-native-image-picker';
代码如下
<Button title="点击启动相机" onPress={() => takePhoto()}></Button>
<Button title="点击启动相册" onPress={() => addPhoto()}></Button>
// 相册选择图片
const addPhoto = () => {
launchImageLibrary(
{
mediaType: 'photo', //'photo'照片, 'video'视频, 'mixed'混合
selectionLimit: 1, // 1为一张,0不限制数量
},
res => {
if (res.assets) {
console.log(res.assets)
}
},
);
};
// 相机拍照
const takePhoto = () => {
launchCamera(
{
mediaType: 'photo', // 'photo'照片, 'video'视频, 'mixed'混合
cameraType: 'back', //'back'后置摄像头, 'front'前置相机
},
res => {
if (res.assets) {
console.log(res.assets)
}
},
);
};