【发布时间】:2020-05-20 05:13:00
【问题描述】:
我正在通过 Expo 学习 React Native。对于一个测试项目,我有一个名为“创建配置文件”的屏幕,我试图在其中添加在 Expo 图像选择器上选择的图像以及其他文本输入 - 位置、描述。如何将输入与图像选择器中的图像结合起来?
我目前的猜测是对图像使用 base64 或将图像输入设置为“this.state.image”以从选取器中调用 image:uri。虽然我可能会走错路。另一种选择是使用 Content-Type:multipart-form/data 尽管在尝试时它不会将任何数据发布到 API 调用。
API 是基于使用 Django Rest Framework 的 Django 应用程序构建的。
我的代码如下: 使成为() { 让 { image } = this.state;
function handleSubmit(props) {
fetch('<API URL HERE>', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body:JSON.stringify({
'location': this.state.location,
'description': this.state.description,
//user image field and input below
'user_image':this.state.image
})
});
}
图片选择器
_pickImage = async () => {
try {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
if (!result.cancelled) {
const image = result.uri;
this.setState({ image: result.uri });
}
console.log(result);
} catch (E) {
console.log(E);
}
};
【问题讨论】:
标签: react-native expo