Vinayak B
Vinayak B

Reputation: 4520

Image from gallery not displaying on img tag in ionic 2

I am trying to display image from gallery to img tag. But image not showing on img tag. But it is working with PhotoViewer . below is my code.

 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
  }

Image captured with

this.camera.getPicture(this.options).then((imageData) => {

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

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

in HTML

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

If I am using sourceType as camera (sourceType: this.camera.PictureSourceType.CAMERA),it also working,it displays image on img tag, But not working on if i use sourceType as sourceType: this.camera.PictureSourceType.PHOTOLIBRARY . Please help

Upvotes: 2

Views: 1995

Answers (1)

Manish
Manish

Reputation: 13577

Hi have the same problem in ios, I resolve this problem by doing the following step

            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 section

<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>

Upvotes: 5

Related Questions