2016-01-14 50 views
0

Issue:我试图以base64格式存储图片,但window.plugins.Base64.encodeFile方法未运行。函数未在window.plugins中运行Angular/Ionic

我已经在本地安装了Cordova插件,$ cordovaImagePicker工作正常。从手机中获取图像后,它只将本地图像路径存储在$ scope.collected.selectedImage中,而不是将其转换为基本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'); 
    }; 

    }); 

回答

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