How to display an image from Ionic native image picker?

删除回忆录丶 提交于 2019-12-11 11:53:57

问题


I'm using Ionic native image picker: https://ionicframework.com/docs/native/image-picker/

I'm importing on my module

import { ImagePicker } from '@ionic-native/image-picker';

And adding on the module's providers

ImagePicker,

On my page I'm importing it

import { ImagePicker, ImagePickerOptions } from '@ionic-native/image-picker';

Adding to my constructor

private imagePicker: ImagePicker,

Then calling a method on a button

async pickImageFromGallery() {
  try {
    const [imageSource] = await this.imagePicker.getPictures(this.pickerOptions);

    this.imgSrc = imageSource;

It does work and I can get the file URI, however when I try to display on an <img> tag the image doesn't show up

<img src="{{ imgSrc }}" alt="" />

Do I need to configure some permission or something? Why doesn't the image show up?


回答1:


Instead of this.imgSrc = imageSource; I did

this.imgSrc = imageSource.replace('file://', '');

Works on both iOS and Android.


If you run into problems like Android crashing try this:

First I cleaned my project

# rm -rf
rimraf .sourcemaps node_modules platforms plugins www

Then installed packages

npm i

Then deploy the app again

ionic cordova run android --device



回答2:


You have to add "" in your src

<img src="{{ imgSrc }}" alt="" />


来源:https://stackoverflow.com/questions/48714500/how-to-display-an-image-from-ionic-native-image-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!