从expo react native将图像上传到Firebase会增加图像大小

时间:2018-12-17 12:17:48

标签: reactjs firebase react-native firebase-storage expo

我面临一个非常奇怪的问题,当我通过expo react native应用程序将图像上传到Firebase时,图像的大小增加了两倍以上。

这是我的代码,如果有人可以提供帮助,将非常感激。

 _pickImage = async () => {
    await this.askPermissionsAsync(); // I have handled the permissions


  var pickerResult = await ImagePicker.launchImageLibraryAsync({
 base64:false,
 aspect:[4,3],
 quality: 0.2,
  });

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

  }
  }


 _handleImagePicked = async pickerResult => {
      this.params = this.props.navigation.state.params;
      var passedParameter = this.params.item.key;

         var uploadUrl = await uploadImageAsync(pickerResult, passedParameter);
          this.setState({ itemImage: uploadUrl });
          console.log("itemIma",uploadUrl)
      }


async function uploadImageAsync(uri,passedParameter) {

// passedParameters是上一页传递的值

  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 timestamp = Date.now()

  const ref = firebase
    .storage()
    .ref()
    .child("CategoryDescription" + "/" + `${passedParameter}` + "/" + `${timestamp}`);

  const snapshot = await ref.put(blob);

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

  return downloadUrl;
}

由于我长期以来一直面临问题,请帮助我,我真的找不到解决办法。

0 个答案:

没有答案