简体   繁体   English

使用电容器和离子显示图像时出错

[英]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.我正在尝试使用 Ionic 4 编写一个简单的相机应用程序,这是我用于捕获图像并将其显示在 Ionic 选项卡中的代码片段。

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在 web 控制台中,我看到了这个

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

The image doesn't show up in devapp as well.该图像也不会出现在 devapp 中。

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.奇怪的是,当我在 Android 手机上进行测试时,图像在磁盘上保存得很好,但它仍然没有显示在页面上。

If you use resultType Uri you have to use capturedImage.webPath to display the url, the dataUrl is undefined unless you se resultType DataUrl如果使用 resultType Uri,则必须使用 captureImage.webPath 来显示 url,除非您设置 resultType DataUrl,否则 dataUrl 是未定义的

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

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