【发布时间】:2019-12-17 03:14:31
【问题描述】:
嘿,我正在尝试将裁剪后的图像上传到 Firebase。 我更喜欢使用离子原生的“图像选择器”和“裁剪”。 我真的不知道如何在裁剪后上传图像,因为它只返回新图像的路径。
我已经尝试过这样的事情。这有效,但我无法裁剪图像。但正如我所提到的,我更喜欢使用原生工具。
export interface UploadData {
name: string;
filepath: string;
size: number;
}
uploadFile(event: FileList) {
// The File object
const file = event.item(0);
// Validation for Images Only
if (file.type.split('/')[0] !== 'image') {
console.error('unsupported file');
return;
}
// The storage path
const path = `whatever/${new Date().getTime()}_${file.name}`;
// File reference
const fileRef = this.storage.ref(path);
// The main task
this.task = this.storage.upload(path, file, { customMetadata });
this.snapshot = this.task.snapshotChanges().pipe(
finalize(() => {
// Get uploaded file storage path
this.UploadedFileURL = fileRef.getDownloadURL();
this.UploadedFileURL.subscribe(resp => {
this.addImagetoDB({
name: file.name,
filepath: resp,
size: this.fileSize
});
}, error => {
console.error(error);
});
}),
tap(snap => {
this.fileSize = snap.totalBytes;
})
);
}
addImagetoDB(image: UploadData) {
const id = this.db.createId();
// Set document id with value in database
this.imageCollection.doc(id).set(image).then(resp => {
console.log(resp);
}).catch(error => {
console.log('error ' + error);
});
}
}
这就是我想要的方式。但我真的不知道,此时如何上传。
pickImage() {
this.imagePicker.getPictures(this.imagePickerOptions).then((results)
=> {
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < results.length; i++) {
this.cropImage(results[i]);
}
}, (err) => {
alert(err);
});
}
cropImage(imgPath) {
this.crop.crop(imgPath, { quality: 50 })
.then(
newPath => {
// ?????
},
error => {
alert('Error cropping image' + error);
}
);
}
对不起,我对这些东西很陌生。 谢谢你的帮助:)
【问题讨论】:
-
似乎没有人会回答这个问题。昨晚我试着写点东西,但是......我真的不知道答案:P 我昨天和今天都试图为你解决这个问题,但没有成功。猜猜你在学习的时候需要从更简单的东西开始。我找不到任何解释这个特定任务链的教程。
-
嗯,好吧,那太伤心了:(但感谢您的尝试
-
是的,一般情况下事情进展得很快,所以如果你没有尽早得到答案,它可能不会到来。不过,我一直在进一步研究。
标签: javascript firebase ionic-framework ionic4 crop