如何将图像对象保存到实时Firebase [英] How To Save Object Of Images To Real-time Firebase

查看:121
本文介绍了如何将图像对象保存到实时Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一堆图像保存到Firebase存储中,并且在 Firebase存储中按逐个图像已知的方式很好地保存了,所以在保存之后,我想获取所有Uri并将其放入实时数据库中像这样的数组对象

I want to save a bunch of Images to Firebase storage and it's saved very well "as known image by image " in Firebase Storage, so after I saved it I want to get all the Uri and put it into Real-time DB as an Array-object like this

但是我在这段代码中尝试过并保存一个像这样的图片!

but I'm tried here in this code and also save one image just like this!

那么如何处理这些

 // Open Gallery
  pickMultiple = () => {
    ImagePicker.openPicker({
      multiple: true
    })
      .then(images => {
        this.setState({
          images: images.map(i => {
            return {
              uri: i.path,
              width: i.width,
              height: i.height,
              mime: i.mime
            };
          })
        });
      })
      .catch(e => console.log(e));
  };
  _SaveImagesToFirebase = () => {
const uid = firebase.auth().currentUser.uid; // Provider
const { images } = this.state;
const provider = firebase.database().ref(`providers/${uid}`);
images.map(image => {
  let file = image.uri;
  const path = "Img_" + Math.floor(Math.random() * 1500 + ".jpg");
  const ref = firebase
    .storage()
    .ref(`provider/${uid}/ProviderGalary/${path}`);
  let imagesArray = [];
  ref
    .put(file)
    .then(() => {
      ref
        .getDownloadURL()
        .then(
          images => {
            console.log(images);
            imagesArray.push({
              uri: images
            });
          },
          error => console.log(error)
        )
        .then(() => {
          provider
            .update({
              Images: imagesArray
            })
            .then(() => console.log("done with imgs"));
        });

      console.log("@inside", imagesArray);
    })
    .then(() => {
      setTimeout(() => {
        this.props.navigation.navigate("Home");
      }, 2000);
    });
  console.log("@OUT", imagesArray);
});
};


推荐答案

UH我不好,我只定义 imagesArray map()里面,应该在外面!像这样,

UH My bad, I just define imagesArray inside map() it should be outside! like this,

 _SaveImagesToFirebase = () => {
    const uid = firebase.auth().currentUser.uid; // Provider
    const { images } = this.state;
    const provider = firebase.database().ref(`providers/${uid}`);
 => let imagesArray = [];
    images.map(image => {
      let file = image.uri;
      const path = "Img_" + Math.floor(Math.random() * 1500 + ".jpg");
      const ref = firebase
        .storage()
        .ref(`provider/${uid}/ProviderGalary/${path}`);
      ref
        .put(file)
        .then(() => {
          ref
            .getDownloadURL()
            .then(
              images => {
                console.log(images);
                imagesArray.push({
                  uri: images
                });
              },
              error => console.log(error)
            )
            .then(() => {
              provider
                .update({
                  Images: imagesArray
                })
                .then(() => console.log("done with imgs"));
            });

        })
        .then(() => {
          setTimeout(() => {
            this.props.navigation.navigate("Home");
          }, 2000);
        });
    });
  };

这篇关于如何将图像对象保存到实时Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆