简体   繁体   中英

Error displaying image using Capacitor and Ionic

I am trying to write a simple Camera app using Ionic 4 and here is my code snippet for capturing the image and displaying it in an Ionic tab.

export class UploadPage {
  yourImage: SafeResourceUrl;

  constructor(private sanitizer: DomSanitizer) {  }

  async captureImage() {
    const capturedImage = await Plugins.Camera.getPhoto(
    {
      quality: 90,
      allowEditing: true,
      source: CameraSource.Camera,
      resultType: CameraResultType.Uri
    });

    this.yourImage = this.sanitizer.bypassSecurityTrustResourceUrl(capturedImage && (capturedImage.dataUrl));

    }
  }

When I try to run the code, it looks like it's capturing the image ok but it's not displaying in the browser. I see this

查看按钮下方

In the web console, I see this

不知道为什么它在 URL 中显示未定义

The image doesn't show up in devapp as well.

The weird thing is, when I tested this out on an Android phone, the image saved fine on the disk but it still doesn't show on the page.

If you use resultType Uri you have to use capturedImage.webPath to display the url, the dataUrl is undefined unless you se resultType DataUrl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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