我正在使用expo-image-picker
上传图像。但问题是我想通过Byte形式通过WebAPI将图像发送到服务器。那么如何将图像URI转换为Byte?如果有人有经验,请与我们分享。
componentDidMount() {
this.getPermissionAsync();
}
getPermissionAsync = async () => {
if (Constants.platform.ios) {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
alert('Sorry, we need camera roll permissions to make this work!');
}
}
};
_pickImage = async () => {
try {
let result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
});
console.log(result);
if (!result.cancelled) {
this.setState({ image: result.uri });
}
} catch (e) {
console.log(e);
}
};
render() {
return (
<View>
<Card>
<CardItem>
<Body>
<Button block primary onPress={() => this._pickImage()}>
<Text>Add Activity</Text>
</Button>
{this.state.image && <Image source={{ uri: this.state.image }} style={{ width: 200, height: 200 }} /> }
</Body>
</CardItem>
</Card>
</View>
);
}
Expo Snack
最佳答案
您可以执行以下操作:
import { Buffer } from "buffer";
_pickImage = async () => {
try {
let result = await ImagePicker.launchImageLibraryAsync({
base64: true,
allowsEditing: true,
aspect: [4, 3],
});
if (!result.cancelled) {
let imageByte = new Buffer(result.base64, "base64");
this.setState({ image: result.uri });
}
} catch (e) {
console.log(e);
}
};
注意:您将需要buffer软件包。
关于javascript - 如何将图像URI转换为字节Expo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59989963/