【问题标题】:In React Native, how to have access to the result of the selection of an image in the image Library?在 React Native 中,如何访问图片库中图片的选择结果?
【发布时间】:2022-11-04 08:41:27
【问题描述】:

我想知道是否存在用于使用 ImagePicker 模块选择图像的“事件挂钩”?

即,当启动ImagePicker时,我们可以访问图像库,并且可以选择一个或多个图像,当一个选择一个图像时,我想计算此图像的Uint8array并将其存储在某个地方。

谢谢

【问题讨论】:

    标签: react-native


    【解决方案1】:

    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
    
    

    【讨论】:

    • @ailauli69 检查新的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    相关资源
    最近更新 更多