通过从firebase存储中提取图像作出本机反应
通过从firebase存储中提取图像作出本机反应,firebase,react-native,Firebase,React Native,uploadPhotoAsync=async uri=>{ const path=`photos/${this.uid}/${Date.now()}.jpg`; //uploadPhotoAsync=(uri,文件名)=>{ 返回新承诺(异步(res,rej)=>{ 常量响应=等待获取(uri); const file=wait response.blob(); 让上传=firebase .储存 .ref(路径)//路径 .put(文件); 上传( “状态已更改”, 快照=>{}, 错误=>{
uploadPhotoAsync=async uri=>{
const path=`photos/${this.uid}/${Date.now()}.jpg`;
//uploadPhotoAsync=(uri,文件名)=>{
返回新承诺(异步(res,rej)=>{
常量响应=等待获取(uri);
const file=wait response.blob();
让上传=firebase
.储存
.ref(路径)//路径
.put(文件);
上传(
“状态已更改”,
快照=>{},
错误=>{
rej(err);
},
异步()=>{
const url=wait upload.snapshot.ref.getDownloadURL();
res(url);
}
);
});
};
您需要存储url,请参阅代码中的注释。
然后,当您需要显示/拉取此图像时,从firebase db获取url
uploadPhotoAsync = async uri => {
const path = `photos/${this.uid}/${Date.now()}.jpg`;
// uploadPhotoAsync = (uri, filename) => {
return new Promise(async (res, rej) => {
const response = await fetch(uri);
const file = await response.blob();
let upload = firebase
.storage()
.ref(path) //path
.put(file);
upload.on(
"state_changed",
snapshot => {},
err => {
rej(err);
},
async () => {
const url = await upload.snapshot.ref.getDownloadURL();
//store this url in firebase db related to the user
//e.g. `${userid}\photo\url`
res(url);
}
);
});
};