2015-11-05 81 views
0

我最近在嘗試保存圖片時遇到了Ionic應用程序的問題。將圖像保存爲Base64時,應用程序在離子視圖中崩潰

我使用ngcordova插件捕獲圖像,並將其作爲base64字符串保存在我的作用域中。

我選擇使用localstorage來存儲我們的圖片,因爲它們會是低質量的圖片,這不是很重要。

的方法拍照

$scope.takePictureFront = function(){ 
    var cameraOptions = { 
    quality: 10, 
    destinationType: Camera.DestinationType.DATA_URL 
    }; 
    var success = function(data){ 
    $scope.$apply(function() { 
     /* 
     remember to set the image ng-src in $apply, 
     i tried to set it from outside and it doesn't work. 
     */ 
     $scope.cameraPicFront = "data:image/jpeg;base64," + data; 
     alert('Inserting front '); 

    }); 
    }; 
    var failure = function(message){ 
    alert('Failed because: ' + message); 
    }; 
    //call the cordova camera plugin to open the device's camera 
    navigator.camera.getPicture(success , failure , cameraOptions); 
}; 

的對象被保存到localStorage的

var newCustomer = { 
    firstname: $scope.customer.firstname(), 
    lastname: $scope.customer.lastname(), 
    title: $scope.customer.title(), 
    phone: $scope.customer.phone(), 
    cellphone: $scope.customer.cellphone(), 
    email: $scope.customer.email(), 
    timeStamp: today, 
    metAt: $scope.conferencePicked, 
    addedBy : $scope.userLoggedIn, 
    imageFront : $scope.cameraPicFront, 
    imageBack : $scope.cameraPicBack, 
    comment: $scope.customer.comment(), 
    interests : $scope.selected 

}; 

如何對象數組被保存到localStorage的

$scope.storedJSON = JSON.parse(window.localStorage['data'] || '{"companies":[],"customers":[]}'); 
     $scope.storedJSON.customers.push(newCustomer); 
     $scope.storedJSON.companies.push(newCompany); 
     var jsonData = JSON.stringify($scope.storedJSON); 
     window.localStorage['data'] = jsonData; 
     $state.go('tab.search'); 

應用程序空閒,然後崩潰或抓住響應,當我嘗試在ipad,平板電腦和Android設備上插入日期。

有什麼建議嗎?

編輯

我發現了一個辦法解決這個。即使是偉大的手機,我嘗試過的平板電腦也存在問題,理解如何將這樣的文件保存在本地存儲中。

我編輯了相機插件的選項,現在正在保存一個較小的圖像,具有較低的taget分辨率,並且一切看起來都很好。只爲任何人在未來磕磕絆絆這個荒謬的問題:)

回答

1

應用程序中可能有內存泄漏。由於我不知道你的應用如何運作,這將是我最好的猜測。你可以嘗試的另一件事是檢查是否允許LocalStorage;當您嘗試使用LocalStorage(個人體驗)時,存在很多內存違規。

我可以推薦你做的是將base64保存到一個文本文件;這不會妨礙您的應用程序的性能,因爲讀取/寫入操作真的是記憶力強。 :)

看一看:

$scope.saveFileToDataStorate = function() { 

    var data = JSON.stringify(newCustomer); 

    $cordovaFile.writeFile(cordova.file.dataDirectory, 'userDetails.txt', data) 
     .then(function(suc){ 

      alert('File Saved! You can now try and open it.'); 

     }, function(error){ alert(error.code); }); 

}; 

請注意,在你的控制器的$cordovaFile依賴。

相關問題