我最近在嘗試保存圖片時遇到了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分辨率,並且一切看起來都很好。只爲任何人在未來磕磕絆絆這個荒謬的問題:)