来自图库的图像未显示在离子2中的img标签上

时间:2017-12-21 07:43:00

标签: android angular image ionic-framework ionic2

我正在尝试将图片从图库显示到img标记。但是图片没有显示在img标签上。但它与PhotoViewer合作。下面是我的代码。

 options: CameraOptions = {
    quality: 100,
    destinationType: this.camera.DestinationType.FILE_URI,
    encodingType: this.camera.EncodingType.JPEG,
    sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
   //mediaType: this.camera.MediaType.PICTURE
  }

使用

捕获的图像
this.camera.getPicture(this.options).then((imageData) => {

  alert(imageData)
  this.photoViewer.show(imageData);
  this.captureDataUrl=imageData;

 }, (err) => {
  // Handle error
 });
HTML中的

<img [src]="captureDataUrl" *ngIf="captureDataUrl"/>

如果我使用sourceType作为摄像头(sourceType: this.camera.PictureSourceType.CAMERA),它也可以工作,它会在img标签上显示图像,但如果我使用sourceType作为sourceType: this.camera.PictureSourceType.PHOTOLIBRARY则不能正常工作。请帮忙

1 个答案:

答案 0 :(得分:4)

你好ios有同样的问题, 我通过执行以下步骤来解决此问题

            var options = {
                  quality: 80,
                  allowEdit: true,
                  sourceType: this.camera.PictureSourceType.CAMERA,
                  saveToPhotoAlbum: false,
                  correctOrientation: true,
                  encodingType: this.camera.EncodingType.JPEG,
                  destinationType: this.camera.DestinationType.FILE_URI
                  //encodingType: this.camera.EncodingType.PNG,
                };


                this.camera.getPicture(options).then((imagePath) => {
                      // Special handling for Android library  // 
                      if (this.platform.is('ios')) {
                        this.ImageData = imagePath.replace(/^file:\/\//, '');
                      }
                      else {
                        this.ImageData = imagePath;
                      }
                      this.photos.push(this.ImageData); //if you have to show multiple image
                      this.photos.reverse();
            }

Html部分

<ion-row>
    <ion-col col-3 *ngFor="let photo of photos; let id = index">
                    <ion-card class="block">
                      <ion-icon name="ios-close-circle-outline" class="deleteIcon" (click)="deletePhoto(id)"></ion-icon>
                      <img [src]="photo" *ngIf="photo" />
                    </ion-card>
    </ion-col>
</ion-row>