离子存储多张照片

时间:2019-08-26 06:10:38

标签: angular ionic-framework ionic3

嗨,我在存储多张照片时遇到问题。因此,基本上,它是从电话库中选择照片,并且可以正常工作。但是,当我要选择另一张照片时,它将代替上一张。

getImages() {
    this.photos = [];
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM
    };

    this.camera.getPicture(options).then(
      imageData => {
        const base64Image = "data:image/jpeg;base64," + imageData;
        const imageName = `image_${this.subject.id}` + ".";
        const component = { imageName, base64Image, type: "image" };
        this.photos.push(component);
        this.photos.reverse();
        this.saveState();
      },
      err => {
        console.log(err);
      }
    );
  }

<ion-row *ngFor="let photo of photos; index as i" nowrap>
    <ion-icon class="subject-body-icon" name="camera" (click)="previewImage(photo)"></ion-icon>
    <span (click)="previewImage(photo)">{{ photo.imageName }}</span>
  </ion-row>
</div>

  <button class="disable-hover transparent-button" ion-button icon-only (click)="getImages()">
    <ion-icon name="cloud-upload"></ion-icon>
  </button>

1 个答案:

答案 0 :(得分:3)

您可以在getImages方法的第一行中初始化photos-array(this.photos = []),这将清除该数组。尝试在离子事件(如pp)中初始化数组

ionViewDidEnter() {
    this.photos = []
  }

加载页面后!

另请参见:https://ionicframework.com/docs/angular/lifecycle