Reactjs 请用户使用react native按钮从照片库或照相机中选择照片
我正在使用expo,无法使用,我想知道如何实现相同的功能。我为每个选项分别提供了以下代码(一个按钮用于gallery,另一个按钮用于camera) 另一个是Reactjs 请用户使用react native按钮从照片库或照相机中选择照片,reactjs,react-native,Reactjs,React Native,我正在使用expo,无法使用,我想知道如何实现相同的功能。我为每个选项分别提供了以下代码(一个按钮用于gallery,另一个按钮用于camera) 另一个是 uploadImage = async() =>{ // Display the camera to the user and wait for them to take a photo or to cancel // the action let result = await ImagePicker.laun
uploadImage = async() =>{
// Display the camera to the user and wait for them to take a photo or to cancel
// the action
let result = await ImagePicker.launchCameraAsync({
allowsEditing: true,
aspect: [4, 3],
});
if (result.cancelled) {
return;
}
// ImagePicker saves the taken photo to disk and returns a local URI to it
let localUri = result.uri;
let filename = localUri.split('/').pop();
// Infer the type of the image
let match = /\.(\w+)$/.exec(filename);
let type = match ? `image/${match[1]}` : `image`;
// Upload the image using the fetch and FormData APIs
let formData = new FormData();
// Assume "photo" is the name of the form field the server expects
formData.append('photo', { uri: localUri, name: filename, type });
return await fetch(YOUR_SERVER_URL, {
method: 'POST',
body: formData,
header: {
'content-type': 'multipart/form-data',
},
});
}
}
这两个函数被称为
<Button title="Pick image" onPress={this._pickImage} />
<Button title="Upload image" onPress={this._uploadImage} />
但是,我如何使我的工作如下所示:世博会已经发布了库,它提供了您所需的确切功能 你需要在答案中发布相关代码,而不仅仅是链接
<Button title="Pick image" onPress={this._pickImage} />
<Button title="Upload image" onPress={this._uploadImage} />