在React Native中,如何访问图像库中图像的选择结果



我想知道是否存在一个;事件挂钩";用于使用ImagePicker模块选择图像的操作?

也就是说,当ImagePicker启动时,我们可以访问图像库,我们可以选择一个或多个图像,当选择一个图像时,我想计算这个图像的uint8数组并将其存储在某个地方。

谢谢

ImagePicker返回对设备上图像数据的图像URI-file:///path/to/local/image.jpg引用。

使用图像URI,使用XMLHttpRequest获取二进制图像数据,并将二进制上传到远程服务器。

const arrayBufferData = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
resolve(xhr.response);
};
xhr.onerror = function (e) {
reject(new TypeError("Network request failed"));
};
// XMLHttpRequest support blob, arraybuffer for  binary data
// blob is recommended while supported by your server.
//
xhr.responseType = "arrayBuffer";
xhr.open("GET", "file:///path/to/local/image.jpg", true);
xhr.send(null);
});
const imageData = new Uint8Array(arrayBufferData);
// You can upload image data now

最新更新