无法在onsen.ui 2项目中加载本地图像

时间:2017-05-31 13:17:37

标签: angular typescript hybrid-mobile-app monaca photoeditorsdk

我创建了一个带有Onsen.ui2框架的Monaca应用程序,其中我正在使用来自ng2-photo-editor的PhotoEditorSDK库 https://www.npmjs.com/package/ng2-photo-editor

在PhotoEidtComponenet.ts(在我的例子中)文件中,我需要指定其assets文件夹的绝对路径。 请检查我的项目视图层次结构的附加图像。

我尝试了不同的路径,如" ../../ utlis / PhotoEditorSdk / assets"等等。, 但是资产中的图标没有加载。

这是资产:{baseUrl:' ./ assets'我需要指定绝对路径。

下面是我的代码我如何尝试加载集成库

import {Component, ViewChild, ViewEncapsulation, EventEmitter, ElementRef} from '@angular/core';
const PhotoEditorSDK = require('../../utils/PhotoEditorSDK/js/PhotoEditorSDK.js');
const PhotoEditorReactUI = require('../../utils/PhotoEditorSDK/js/PhotoEditorReactUI.js');
const UnsplashProvider = require('../../utils/PhotoEditorSDK/js/UnsplashProvider.js');

@Component({
      selector: 'ons-page[page]',
    encapsulation: ViewEncapsulation.None,
    template: `
        <div #host id="photo-host" style="min-height: 100px; min-width:100px"></div>
        <img src = "app/img/test.jpg">
        <div class="actions">
                 <a class="btn white-btn active" (click)="cancelClick()">Discard</a>
            <a class="btn white-btn pull-right" (click)="saveClick()">Save</a>
          </div>`,
        styles: [require('../../utils/PhotoEditorSDK/css/PhotoEditorReactUI.css') ]

})

export class PhotoEdit{

  @ViewChild('host') host: any;
  el: ElementRef;
  editor: any;
  propagateChange = (_: any) => { };


  constructor(el: ElementRef) {
      this.el = el;
  }

  ngOnInit() {
      // this.createEditor();
  }

  ngAfterViewInit() {
    setTimeout(() => this.createEditor(), 1);
  }

  ngOnDestroy() {
      if (this.editor) {
          this.editor.dispose();
      }
  }

  ngOnChanges(changes: any) {
  //        console.log(changes);
      if (this.editor) {
          this.editor.dispose();
      }
      this.createEditor();
      this.propagateChange(changes);
  }

  writeValue(value: any) {
      //console.log(value);
  //        if (this.editor) {
  //            if (value && value !== '') {

  //            }
  //        }
  }
  registerOnChange(fn: any) {
      this.propagateChange = fn;
  }
  registerOnTouched(fn: any) { }

  createEditor() {
      let myImage = new Image();
      myImage.crossOrigin = 'anonymous';
      myImage.setAttribute('crossOrigin', 'anonymous');
          this.editor = new PhotoEditorSDK.UI.ReactUI({
              container: this.host.nativeElement,
              logLevel: 'info',
            title: '',
            showTopBar: false,
            showCloseButton: false,
            editor: {
                image: myImage
            },
            photoRoll: {
                provider: UnsplashProvider
            },
            assets: {
                baseUrl: './assets'

            },
            ui: {
                enabled: true,
                showExportButton: true,
                export: {
                    type: PhotoEditorSDK.RenderType.DATAURL,
                    download: false
                }
            },
            responsive: true,
            language: 'en'
          });
      myImage.src = "./test.jpg";
  }

  saveClick() {
      this.editor.export(false)
          .then(result => {
              // this.saveImageEvent.emit(result);
          });
  }

  cancelClick() {
      // this.cancelEvent.emit();
  }
}

我尝试加载一个简单的图像

任何帮助都表示赞赏。 提前致谢enter image description here

0 个答案:

没有答案
相关问题