函数没有在Angular / Ionic中的window.plugins中运行

时间:2016-01-14 13:44:04

标签: javascript angularjs cordova plugins ionic

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

我在本地安装了Cordova插件,$ 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');
    };

  });

1 个答案:

答案 0 :(得分:0)

在关于这个插件的所有教程中,他们将结果(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