gpt4 book ai didi

javascript - 如何将图像URI转换为字节Expo

转载 作者:行者123 更新时间:2023-12-03 10:02:51 25 4
gpt4 key购买 nike

我正在使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com