在 Ionic 3 中显示由 Native Camera 拍摄的 FILE_URI 图像 [英] Displaying a FILE_URI image taken by Native Camera in Ionic 3

查看:20
本文介绍了在 Ionic 3 中显示由 Native Camera 拍摄的 FILE_URI 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Ionic 3 中显示用户使用 @ionic-native/camera 拍摄的 FILE_URI 图像?

How do you display a FILE_URI image taken by the user using @ionic-native/camera in Ionic 3?

我可以使用 Ionic Native 的 Camera 获取 FILE_URI 图片 URL,结果如下:

I can use Ionic Native's Camera to get a FILE_URI image URL, with a result like this:

file:///var/mobile/Containers/Data/Application/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/tmp/cdv_photo_005.jpg

但是,当我尝试通过引用视图模板中的 URI 来将此图像显示给用户时,图像永远不会加载.

However, When I try to display this image back to the user by referring to the URI in my view template, the image never loads.

我尝试过的事情:

-在视图中直接使用图片URI

-Using the image URI directly in the view

<img src="{{profile.image}}">    // Never loads

-通过在页面组件中包含 DomSanitizer 来清理 URI:

-Sanitizing the URI by including DomSanitizer in the page component:

<img [src]="domSanitizer.bypassSecurityTrustUrl(profile.image)">    // Never loads

由于性能拖累,我宁愿不使用 base64 图像.我在这里做错了什么?

I would rather not use a base64 image because of the performance drag. What am I doing wrong here?

推荐答案

import { normalizeURL } from 'ionic-angular';ionic3 <img>标签源

import { normalizeURL } from 'ionic-angular'; ionic3 <img> tag src

<img *ngIf="base64Image" src="{{base64Image}}"/> 

 openCamera(pictureSourceType: any) {
  let options: CameraOptions = {
   quality: 95,
   destinationType: this.camera.DestinationType.FILE_URI,
   sourceType: pictureSourceType,
   encodingType: this.camera.EncodingType.PNG,
   targetWidth: 400,
   targetHeight: 400,
   saveToPhotoAlbum: true,
   correctOrientation: true
 };
 this.camera.getPicture(options).then(imageData => {
  if (this.platform.is('ios')) {
      this.base64Image = normalizeURL(imageData);
      // Alternatively if the problem only occurs in ios and normalizeURL 
      // doesn't work for you then you can also use:
      // this.base64Image= imageData.replace(/^file:///, '');
  }
  else {
      this.base64Image= "data:image/jpeg;base64," + imageData;
  }
 }, error => {
     console.log('ERROR -> ' + JSON.stringify(error));
   });
 }

这篇关于在 Ionic 3 中显示由 Native Camera 拍摄的 FILE_URI 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆