【问题标题】:function not running in window.plugins in Angular/Ionic功能未在 Angular/Ionic 的 window.plugins 中运行
【发布时间】:2016-01-14 13:44:04
【问题描述】:

问题:我正在尝试以 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');
    };

  });

【问题讨论】:

    标签: javascript angularjs cordova plugins ionic


    【解决方案1】:

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

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

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

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

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

    您的 results[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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多