首页 > 解决方案 > 从图库中选择图像时 Ionic3 应用程序崩溃

问题描述

我正在使用图像选择器插件来获取图像表单库。因此,如果用户先拍照,然后使用图库获取图片,则它可以正常工作。

但是,如果用户在没有(使用相机)的情况下选择直接图库,则应用程序崩溃而没有任何错误。

 //Get options on device if camera or gallery
  presentActionSheet() {
 let actionSheet = this.actionSheetCtrl.create({
   title: 'Choose or take a picture',
   buttons: [
  {
    text: 'Take a picture',
    handler: () => {
      this.takePicture();
    }
  },
  {
    text: 'Choose pictures',
    handler: () => {
      this.openImagePicker();
    }
  }
]
 });
actionSheet.present();
 }

图像选择器的功能在这里。

//Open image picker multiple images
  openImagePicker(){
this.pic = 'undefined';
let options = {
  maximumImagesCount: 1,
}
//initalize readAsArrayBuffer for base64 and images gallery to display on view
this.photos = new Array<string>();
this.base64Data = new Array<string>();
this.imagePicker.getPictures(options)
.then((results) => {
  this.reduceImages(results).then(() => {
    console.log('all images cropped!!');
    for (let index = 0; index < results.length; index++) {
        //here iam converting image data to base64 data and push a data to array value.
        console.log('Seelcted images are');
        //console.log(this.imageSelected);
        this.base64.encodeFile(results[index]).then((base64File: string) => {
        //Create a base64 array for server transfer
        this.base64Data.push(base64File);
  }, (err) => {
          console.log(err);
        });

      }
    //console.log("Image Lists", this.photos);
  });
   }, (err) => { console.log(err) });
  }

我正在使用的插件

import { Camera } from '@ionic-native/camera';
import { ImagePicker } from '@ionic-native/image-picker';
import { Crop } from '@ionic-native/crop';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file- 
 transfer';
import { FilePath } from '@ionic-native/file-path';
import { File } from '@ionic-native/file';
import { Base64 } from '@ionic-native/base64';

这有什么问题?我正在使用离子3

标签: ionic3

解决方案


我也遇到了这个问题,似乎有些内存泄​​漏。

然后我尝试了 Cordova 插件的分叉版本,它工作正常。

https://github.com/abelabbesnabi/cordova-plugin-image-picker

推荐阅读