首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ionic3和FabricJS

Ionic3和FabricJS
EN

Stack Overflow用户
提问于 2018-01-29 03:44:15
回答 1查看 1.1K关注 0票数 1

我对Ionic3和FabricJS有很大的问题。它向我展示了html中的图像,但当我尝试将其保存并上传到Firebase时,它只会上传一个白色屏幕,就像新画布一样。

这是我的代码

代码语言:javascript
复制
import { Component, NgZone } from '@angular/core';
import { NavController, NavParams, AlertController } from 'ionic-angular';
import firebase from 'firebase';
import { HomePage } from '../home/home';

import 'fabric';
declare const fabric: any;

import { ViewChild } from '@angular/core';
import { Slides } from 'ionic-angular';

@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");
}


@ViewChild(Slides) slides: Slides;

goToSlide() {
      this.slides.slideTo(2, 500);
    }

ionViewDidLoad(){

  let width = (window.innerWidth > 0) ? window.innerWidth : screen.width;
  let height = (window.innerHeight > 0) ? window.innerHeight : screen.height;
  let altezza = height / 100 * 60;

  canvas = new fabric.Canvas("c");

  canvas.setDimensions({ width: width, height: altezza});
  fabric.Image.fromURL(this.fotoScelta, function(img) {
    img.scaleToWidth(canvas.getWidth());
    canvas.add(img);
    canvas.renderAll();
    });

this.fotoModificata = canvas.toDataURL("png");

}

upload() {

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

    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}.png`).getDownloadURL().then((url) => {
           this.zone.run(() => {
             this.imgsource = url;

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


            })
         });
        this.showSuccesfulUploadAlert();

       }, (err) => { });

  }


goToHome() {
    this.navCtrl.setRoot(HomePage);
  }


showSuccesfulUploadAlert() {

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

      this.fotoModificata = "";
      //this.goToHome();

  }

}

this.fotoModificata是我为upload函数设置的变量。this.fotoScelta是一个变量,它包含我的图像的base64字符串。这是我的HTML代码

代码语言:javascript
复制
<ion-list>
  <canvas id="c"></canvas>
</ion-list>

如果我要求,它会在控制台中打印一个base64代码,但它是一个空白图像。

我怎么才能修复它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-29 17:43:22

代码语言:javascript
复制
let self = this;
fabric.Image.fromURL(this.fotoScelta, function(img) {
  img.scaleToWidth(canvas.getWidth());
  canvas.add(img);
  canvas.renderAll();
  self.fotoModificata = canvas.toDataURL({
    format: "png"
  });
});

在图片加载回调中使用toDataURL,这样就会在导出的png中包含图片。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48490591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档