2016-01-14 102 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