函数未在 window.plugins in Angular/Ionic 中运行

function not running in window.plugins in Angular/Ionic

本文关键字:Ionic 运行 Angular in window plugins 函数      更新时间:2023-09-26

问题:我正在尝试以base 64格式存储图片,但window.plugins.Base64.encodeFile方法未运行。

我已经在本地安装了科尔多瓦插件,$cordovaImagePicker工作正常。 从手机获取图像后,它仅将本地图像路径存储在 $scope.collected.selectedImage 中,而不是将其转换为 base 64 格式。

感谢您的帮助!!

'use strict';
angular.module('copula')
  .controller('ItemsCtrl', function($scope, $state, Auth, Item, $firebaseObject, $cordovaImagePicker, $ionicPlatform) {
    var ref = new Firebase('https://copula.firebaseio.com/users');
    var authData = Auth.$getAuth();
    var itemsObject = $firebaseObject(ref.child(authData.uid + '/items'));
    itemsObject.$loaded().then(function() {
      $scope.items = itemsObject;
    });
    $scope.collection = {
      selectedImage: ''
    };
    $scope.item = {};
    $ionicPlatform.ready(function() {
        $scope.getImageSaveContact = function() {
            // Image picker will load images according to these settings
            var options = {
                maximumImagesCount: 1, // Max number of selected images, I'm using only one for this example
                width: 800,
                height: 800,
                quality: 80            // Higher is better
            };
            $cordovaImagePicker.getPictures(options).then(function (results) {
                $scope.collection.selectedImage = results[0];   // We loading only one image so we can use it like this
                window.plugins.Base64.encodeFile($scope.collection.selectedImage, function(base64){  // Encode URI to Base64 needed for contacts plugin
                    console.log("before encoding");
                    $scope.collection.selectedImage = base64;
                    console.log(base64);
                });
                console.log($scope.collection.selectedImage);
            }, function(error) {
                console.log('Error: ' + JSON.stringify(error));    // In case of error
            });
        };
    });
    $scope.$on('$ionicView.beforeEnter', function (event, viewData) {
      viewData.enableBack = true;
    });
    $scope.submitItem = function() {
      ref.child(authData.uid).child('items').push(Item.pushAttrs($scope.item));
      $scope.item = {};
      $state.go('main.home');
    };
  });

在关于此插件的所有教程中,他们将结果(base64)用于ng-src,如下所示:

<img ng-src="{{collection.selectedImage}}">

这意味着这个插件正在返回一个路径,这是这个插件的正常行为。

console.log('file base64 encoding: ' + base64);

它返回转换后的base64 img的路径。

您的结果[0] 路径和 base64 路径应该不同。

来源 : http://www.gajotres.net/accessing-image-galery-using-ionic-and-ngcordova/2/

资料来源:https://forum.ionicframework.com/t/how-to-load-image-from-android-local-storage/25132/3