代码之家  ›  专栏  ›  技术社区  ›  Bhavin

IONIAL 3未在图像src中显示相机文件的URI路径

  •  4
  • Bhavin  · 技术社区  · 6 年前

    这是我的HTML代码

    <img [src]="profileImage"/>
    <button (click)="openCamera()"> Upload </button>
    

    import { ViewController, Platform, normalizeURL } from 'ionic-angular';
    
    openCamera() {
    let options = {
        quality: 100,
        destinationType: this.camera.DestinationType.FILE_URI,
        encodingType: this.camera.EncodingType.JPEG,
        mediaType: this.camera.MediaType.PICTURE,
        saveToPhotoAlbum: false,
        targetWidth: 400,
        targetHeight: 400,
        allowEdit: false
    }
    
     this.camera.getPicture(options).then((imageData) => {
        this.cropService.crop(imageData, { quality: 100 }).then((newImage) => {
            this.profileImage = normalizeURL(newImage);
        }, (error) => {
            console.log("Error cropping image", error);
        });
    }, (err) => {
        console.log('Error camera image', err);
    });
    }
    
    1. 当我从相机捕捉图像时,它会返回文件URI,如下所示 ,
    2. 但图像没有显示在 <img>
    1 回复  |  直到 6 年前
        1
  •  2
  •   Sandy.....    6 年前

    我修改了你的密码。请在下面查找更新的代码:

    import { ViewController, Platform, normalizeURL } from 'ionic-angular';
    
    openCamera() {
        let options = {
            quality: 100,
            destinationType: this.camera.DestinationType.FILE_URI,
            encodingType: this.camera.EncodingType.JPEG,
            mediaType: this.camera.MediaType.PICTURE,
            saveToPhotoAlbum: false,
            targetWidth: 400,
            targetHeight: 400,
            allowEdit: false
        }
    
         this.camera.getPicture(options).then((imageData) => {
            this.cropService.crop(imageData, { quality: 100 }).then((newImage) => {
                if (this.platform.is('ios')){
                    this.profileImage = normalizeURL(newImage);
                }else{
                    this.profileImage= "data:image/jpeg;base64," + newImage;
                }
            }, (error) => {
                console.log("Error cropping image", error);
            });
        }, (err) => {
            console.log('Error camera image', err);
        });
    }
    


    如果上述解决方案不起作用,请尝试以下代码:

        import { ViewController, Platform, normalizeURL } from 'ionic-angular';
    
        openCamera() {
          let options = {
            quality: 100,
            destinationType: this.camera.DestinationType.FILE_URI,
            encodingType: this.camera.EncodingType.JPEG,
            mediaType: this.camera.MediaType.PICTURE,
            saveToPhotoAlbum: false,
            targetWidth: 400,
            targetHeight: 400,
            allowEdit: false
        }
    
        this.camera.getPicture(options).then((imageData) => {
              // don't need to crop image for iOS platform as it is a in-build feature
              if (this.platform.is('ios')) {
                this.getFileUri(imageData);
              }else { // android platform, we need to do manually
                this.cropService.crop(imageData, { quality: 100 }).then(newImage => {
                    this.getFileUri(newImage);
                  },
                  error => console.error('Error cropping image', error)
                );
             }
        }, (err) => {
            console.log('Error camera image', err);
        });
    }
    
    private getFileUri = (url: any) => {
        var scope = this;
        var xhr = new XMLHttpRequest();
        xhr.onload = function() {
          var reader = new FileReader();
          reader.onloadend = function() {
            scope.profileImage = reader.result;
          }
          reader.readAsDataURL(xhr.response);
        };
        xhr.open('GET', url);
        xhr.responseType = 'blob';
        xhr.send();
    }
    


    1] 卸载现有的webview插件

    ionic cordova plugin rm cordova-plugin-ionic-webview
    

    2] 安装旧版本

    ionic cordova plugin add cordova-plugin-ionic-webview@1.2.1
    

    3] 清洁cordova android

    ionic cordova clean android
    

    请在此处查找更多详细信息 https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/158

        2
  •  0
  •   Cerlin    6 年前

    saveToPhotoAlbum: false
    

    saveToPhotoAlbum: true