通过expo图像选择器上传到Firebase时,使用XMLHttpRequest进行本机响应时,文件图像大小会增加

时间:2019-01-09 16:39:13

标签: image firebase react-native expo

我面临一个非常奇怪的问题,即在上传到Firebase时,我的图片大小会增加。 我已在上传之前记录了文件大小,它显示的大小相同,但是上传后文件大小增加了。图像也未损坏。请帮忙。

上传到Firebase代码:

 async uploadImageAsync(uri, passedParameter, ItemName) {
    const blob = await new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      xhr.onload = function() {
        resolve(xhr.response);
      };
      xhr.onerror = function(e) {
        console.log(e);
        reject(new TypeError("Network request failed"));
      };
      xhr.responseType = "blob";
      xhr.open("GET", uri, true);
      xhr.send(null);
    });

    const ref = firebase
      .storage()
      .ref()
      .child("CategoryDescription" + "/" + passedParameter + "/" + ItemName);

    const snapshot = await ref.put(blob);

    blob.close();
    const downloadUrl = await snapshot.ref.getDownloadURL();

    return downloadUrl;
  }

**选择图片代码:**

 async _pickImage() {
    const { CAMERA, CAMERA_ROLL } = Permissions;
    const permissions = {
      [CAMERA]: await Permissions.askAsync(CAMERA),
      [CAMERA_ROLL]: await Permissions.askAsync(CAMERA_ROLL)
    };

    if (
      permissions[CAMERA].status === "granted" &&
      permissions[CAMERA_ROLL].status === "granted"
    ) {
      var pickerResult = await ImagePicker.launchImageLibraryAsync({
        quality: 0.1
      });

      if (!pickerResult.cancelled) {
        console.log(pickerResult);
        this.setState({
          itemImage: pickerResult.uri
        });

**在按钮上按下的代码**

  handleConfirmAddItems = () => {
    var passedParameter = this.params.item.key;
    const { ItemName, ItemPrice, ItemDesc, ItemWeight, itemImage } = this.state;


      {
        itemImage !== "" &&
          this._handleImagePicked(itemImage, passedParameter, ItemName);
      }

      writeUrlToDB(
        itemImage,
        passedParameter,
        ItemName,
        ItemDesc,
        ItemPrice,
        ItemWeight
      );
    }
  };

**处理选择的图像**

 async _handleImagePicked(itemImage, passedParameter, ItemName) {
    try {
      const uploadUrl = await this.uploadImageAsync(
        itemImage,
        passedParameter,
        ItemName
      );
      this.setState({ itemImage: uploadUrl });
      console.log("itemIma", uploadUrl);
    } catch (e) {
      console.log(e);
      alert("Upload failed, sorry :(");
    }
  }

请帮助。.我压力很大

1 个答案:

答案 0 :(得分:0)

图像选择器中的错误。这是iOS模拟器的问题,在Android设备中可以正常工作:)