繁体   English   中英

如何在不调用 imageChangedEvent 事件的情况下在 ngx-image-cropper 上加载图像?

[英]How to load the image on the ngx-image-cropper without calling the imageChangedEvent event?

我开发了一个基于 ngx-image-cropper 的裁剪器,它允许裁剪/旋转一些图像。 对于sprint的demo,我希望在应用程序启动时显示图像,所以我需要在不调用fileChangeEvent()事件的情况下在ngx-image-cropper上启动图像。

这是实际结果,它工作正常。

我单击其中一个链接(照片 - 签名 - ...),然后打开文件上传器以选择要裁剪的照片。

在此处输入图片说明

这就是我想要的:

在开始访问页面时,从我的磁盘加载文件,而无需单击链接。

在此处输入图片说明

Ps:我正在使用 Angular 8

这里是裁剪器的代码:

       <mat-card-content style="padding:3% 5%; text-align: center; ">
          <image-cropper [ngClass]=" loaded && showCropper && !cropped ? 'showCropper' : 'hideCropper' "
            [imageChangedEvent]="imageChangedEvent"
            [maintainAspectRatio]="false"
            [containWithinAspectRatio]="containWithinAspectRatio"
            [aspectRatio]="5 / 3"
            [cropperMinWidth]="128"
            [onlyScaleDown]="true"
            [roundCropper]="false"
            [canvasRotation]="canvasRotation"
            [transform]="transform"
            [alignImage]="'center'"
            [style.display]="showCropper ? null : 'none'"
            format="png"
            (imageCropped)="imageCropped($event)"
            (imageLoaded)="imageLoaded()"
            (cropperReady)="cropperReady($event)"
            (loadImageFailed)="loadImageFailed()"
          >
          </image-cropper>
          <img
            *ngIf="loaded && !showCropper && cropped"
            class="document-photo"
            [src]="croppedImage"
          />
          <div *ngIf="!loaded" class="divNone"></div>
          <div class="icon-image">
            <mat-icon (click)="rotateLeft()">rotate_left</mat-icon>
            <mat-icon (click)="rotateRight()">rotate_right</mat-icon>
            <mat-icon (click)="activateCrop()">crop</mat-icon>
            <mat-icon (click)="clearImage()">clear</mat-icon>
            <mat-icon (click)="validate()">check</mat-icon>
            <mat-icon>note_add</mat-icon>
          </div>
        </mat-card-content>

        <mat-list-item class="item-doc text-blue-in">
              <mat-icon class="icon-info" matTooltipPosition="above" matTooltip="Info about the photo">info</mat-icon>
              <mat-icon *ngIf="!photo" class="text-grey-in">check_box_outline_blank</mat-icon>
              <mat-icon *ngIf="photo" class="text-grey-in">check_box</mat-icon>
              <input style="visibility: hidden; display: none;" #linkPhoto type="file" (change)="fileChangeEvent($event, 'photo')"/>
              <mat-label class="doc-title text-grey-in" (click)="onLoadPhoto()" >Photo</mat-label>
              <mat-icon *ngIf="photoSent" class="icon-edit" (click)="editPhoto()">edit</mat-icon                  >
        </mat-list-item>

所以问题是:如何在 ngx-image-cropper 中从磁盘加载图像而不调用 fileChangedEvent ?

1)如果您不想使用上传器/打开窗口(从输入文件)选择图像并从项目目录(例如资产/图像)加载图像将其作为字符串(base64)输入,则必须添加一个输入:

[imageBase64] = "imageBase64String"

其中 imageBase64String 不是 (../../assets/image.jpg)

但是base64的值(date:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQA.....)

2)作者写道:

“所有输入都是可选的。应该设置 imageChangedEvent、imageBase64 或 imageFile 以将图像加载到裁剪器中。”

输入 imageBase64(字符串):

“如果你不想使用文件输入,你可以直接设置一个base64图像,它会被加载到裁剪器中”

3)为了在控制台中没有错误:

"ERROR Error: Uncaught (in promise): Event: {" isTrusted ": true}"

最好的添加是 ngIf(所有可能看起来像这样):

<image-cropper
  *ngIf="imageBase64String"
  [imageChangedEvent]="imageChangedEvent"
  [imageBase64]="imageBase64String"
  ...
  ></image-cropper>

4)以及如何添加base64?

您可以创建一个函数,例如getBase64FromFile(img) {...}并使用XMLHttpRequest() + FileReader()并将结果分配给this.imageBase64String = (base64 as any).result

当 ngx-image-cropper 加载时,例如

ngAfterViewInit (): void {
  this.getBase64FromFile('../../assets/image.jpg');
}

看,这里很有趣http://www.programmersought.com/article/52582038406/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM