【问题标题】:Modify an image using caman and then upload it to firebase使用 caman 修改图像,然后将其上传到 firebase
【发布时间】:2018-01-23 19:09:05
【问题描述】:

这是我的问题。我想使用 caman 修改图像,然后将其上传到 firebase。这是我的代码

import { Component, NgZone } from '@angular/core';
import { NavController, NavParams, AlertController } from 'ionic-angular';
import firebase from 'firebase';

declare var Caman: any;

@Component({
  selector: 'page-filtri',
  templateUrl: 'filtri.html'
})
export class FiltriPage {

fotoModificata: any;
public fotoScelta;
itemRef : firebase.database.Reference = firebase.database().ref('/matrimonio1');
firestore = firebase.storage();
alertCtrl: AlertController;
imgsource: any;

  constructor(public navCtrl: NavController, public navParams: NavParams, alertCtrl: AlertController,   public zone: NgZone,
) {

    this.alertCtrl = alertCtrl;
    this.fotoScelta = navParams.get("foto");
  }

  addFilter(){

       Caman("#image", function(){
         this.brightness(5);
         this.render(function () {
         var fotoModificata = this.toBase64();

   });

 });

}


upload() {


    let storageRef = firebase.storage().ref();
    const filename = Math.floor(Date.now() / 1000);
    const imageRef = storageRef.child(`images/${filename}.jpg`);

    imageRef.putString(this.fotoModificata, firebase.storage.StringFormat.DATA_URL).then((snapshot)=> {

// il codice sotto prende l'url della foto appena caricata
         this.firestore.ref().child(`images/${filename}.jpg`).getDownloadURL().then((url) => {
           this.zone.run(() => {
             this.imgsource = url;

// carica l'url in firebase.database
      let newPostRef = this.itemRef.push();
          newPostRef.set({
            "nome" : " ",
            "url" : url
          });


            })
         });

         this.showSuccesfulUploadAlert();


       }, (err) => { });

  }

  showSuccesfulUploadAlert() {

      let alert = this.alertCtrl.create({
        title: 'Caricata!',
        subTitle: 'La foto è stata caricata!',
        buttons: ['OK']
      });
      alert.present();

      this.fotoModificata = "";

  }

}

我确信 upload() 函数有效。我可以从 addFilter 函数中导出 var fotoModificata 以在 upload() 中使用吗?如果我在 var 声明后立即使用 console.log,我可以在控制台中看到我的图像的 base64 字符串,但如果我在其他地方登录控制台,我已经和未定义。我该如何解决?

【问题讨论】:

  • 我猜你只需要添加来改变这一行:var fotoModificata = this.toBase64();成为 this.fotoModificata = this.toBase64();但要做到这一点,您需要确保“this”指向您的整个组件。为此我会使用箭头函数:Caman("#image", () => { ... }
  • 如果有帮助,请尝试告诉我:) 我也在学习,但我认为这对你有用。
  • 如果我使用它,我在编译时会出错,说亮度和渲染不是 filtripage 的属性
  • 我试过这个:'addFilter(){ Caman("#image", function () { this.brightness(5); this.render( () => { this.fotoModificata = this. toBase64(); }); }); }' 但是当我调用 upload() 我有 ERROR 错误:[object Object]
  • 等一下:) 我想我知道出了什么问题——忽略我之前的评论。我将在 3 分钟内发布工作代码

标签: typescript firebase ionic-framework ionic3 camanjs


【解决方案1】:

好吧,这感觉就像您需要的只是将 addFilter 函数的结果(当前存储在 var fotoModificata 中)传递给全局 var (fotoModificata)。

我会在下面这样做:

addFilter() {

    // save scope of global "this" here:
    let scope = this;

    Caman("#image", function(){
      this.brightness(5);
      this.render(function() {
      // use the scope as a pointer to the global scope to assign the value:
      scope.fotoModificata = this.toBase64();
      });
    });
  }

【讨论】:

  • 仍有 ERROR 错误:控制台中的 [object Object] 使用 html 中的按钮调用 upload()。另外,如果我在 this.fotoModificata = temp; 之后写console.log(this.fotoModificata);我在控制台中检索了一个未定义的值,所以我认为 temp 的值仍然以某种方式在 Caman 函数中被监禁。
  • 好的,我将复制您的代码并查看它是否可以正常工作。您是否设法从您的代码成功登录到控制台 base64?
  • 基本上 - 让我知道如果 ytou log 你会得到什么 -> addFilter 功能中的 var fotoModificata
  • 我认为我的示例现在是错误的,因为 var temp 永远不会“成功”到函数中......
  • 顺便说一句,您检查渲染功能是同步还是异步?可能是它需要异步处理。这可以解释为什么 temp 未定义...
猜你喜欢
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
  • 2017-04-15
  • 2022-08-13
  • 1970-01-01
  • 2019-07-24
相关资源
最近更新 更多