【发布时间】:2020-02-08 00:31:22
【问题描述】:
用例:
用户从相机胶卷中选择多张图片,并将它们添加到他们的帖子中。他们键入描述和标题,然后选择发布。他们的帖子出现在他们的用户个人资料中,并附有他们选择的图像。
问题:
我正在使用 Ionic 4 AngularFire2 创建一个图像博客应用程序。 我的目标是将用户图像上传到 Firebase 存储并下载每个图像 url 以在 Firestore 数据库中引用。
我可以将多张图片上传到 Firebase 存储,但我想下载这些图片网址中的每一个。到目前为止,我只能下载一个图片网址。我是 Ionic/AngularFire 的新手,我不知道要循环什么或应该在哪里循环以获取数组中的图像 url。
打字稿:
async uploadImageToFirebase(image){
let image_src = this.webview.convertFileSrc(image);
let randomId = Math.random().toString(36).substr(2, 5);
//uploads img to firebase storage
this.firebaseService.uploadImage(image_src, randomId)
.then(photoURL => {
this.image = [ photoURL ]
loading.dismiss();
toast.present();
}, err => {
console.log(err);
})
服务:
encodeImageUri(imageUri, callback) {
var c = document.createElement('canvas');
var ctx = c.getContext("2d");
var img = new Image();
img.onload = function () {
var aux:any = this;
c.width = aux.width;
c.height = aux.height;
ctx.drawImage(img, 0, 0);
var dataURL = c.toDataURL("image/jpeg");
callback(dataURL);
};
img.src = imageUri;
};
uploadImage(imageURI, randomId){
return new Promise<any>((resolve, reject) => {
let storageRef = firebase.storage().ref();
let imageRef =
storageRef.child('image').child(randomId);
this.encodeImageUri(imageURI, function(image64){
imageRef.putString(image64, 'data_url')
.then(snapshot => {
snapshot.ref.getDownloadURL()
.then(res => resolve(res))
}, err => {
reject(err);
})
})
})
}
我的 firestore 数据库目前看起来像这样
posts : {
Title : this is a title,
Description: this is a description,
image:
[ 0 ] "https://firebasestorage.googleapis.com/v0/b/imagepost-1962c.appspot.com/o/image%2Fbak95?alt=media&token=58eb6037-4253-4dcc-b35b-8d58ddsdffss"
}
但我希望它看起来像
posts : {
Title : this is a title,
Description: this is a description,
image:
[ 0 ] "https://firebasestorage.googleapis.com/v0/b/imagepost-1962c.appspot.com/o/image%2Fbak95?alt=media&token=58eb6037-4253-4dcc-b35b-8d58ddsdffss"
[ 1 ] "another image url"
[ 2 ] "another image url"
}
【问题讨论】:
标签: angular typescript firebase ionic-framework firebase-storage