How to store images in a Javascript object?

2

I'm developing an app that consists of a map and a camera. At the moment, I want to add the possibility of the user taking a photo with the camera and mark the map where this photo was taken, but I have a hard time storing the photo in the object that stores the information of the places saved on the map. I'll put some code snippets that I consider to be key:

addLocation.html snippet:

<label class="item item-input">
      <span class="input-label">Name</span>
      <input type="text" ng-model="newLocation.name" placeholder="What's Here?" ng-minlength="1" ng-maxlength="100" required>
    </label>

    <label class="item item-input">
      <span class="input-label">Picture</span>
      <button class="button icon-left ion-image button-calm" ng-click="selectPhoto()">Select an image</button>
    </label>

      <div>
        <img id="photo" height="256" width="256">
      </div>


    <div class="item tabs tabs-secondary tabs-icon-left">
      <a class="tab-item" href="#">
        LAT : {{newLocation.lat | truncate:15}}
      </a>
      <a class="tab-item" href="#">
        LNG : {{newLocation.lng | truncate:15}}
      </a>

app.js excerpt: selectPhoto () function:

    $scope.selectPhoto = function() {
    Camera.getPicture({
      quality: 75,
      destinationType: 1,
      sourceType: 0,
      correctOrientation: true
    }).then(function(imageURI) {
        var image = document.getElementById('photo');
        image.src = imageURI;
        image.style.margin = "0 auto";
        image.style.display= "block";
        $scope.newLocation.photo = new Image();
        $scope.newLocation.photo.src = imageURI;
     },function(err) {
      console.err(err);
    });
  }

section of mapController.js:

 $scope.saveLocation = function() {
    LocationsService.savedLocations.push($scope.newLocation);
    $scope.modal.hide();
    $scope.goTo(LocationsService.savedLocations.length - 1);
  };

var location = LocationsService.savedLocations[locationKey];

$scope.map.markers[locationKey] = {
      lat:location.lat,
      lng:location.lng,
      message: location.name + location.photo,
      focus: true,
      draggable: false
    };

In the selectPhoto () function, after selecting the photo of the album, the source of the variable 'image' is being changed and in html the selected image appears. However, I can not save the same image in newLocation.photo, which gets undefined even after selecting the photo. I believe the error is in

$scope.newLocation.photo = new Image();
$scope.newLocation.photo.src = imageURI;

from app.js

I think it's not something complex to solve and that I'm probably doing some simple nonsense, since I'm a beginner in JS, html, etc. But I really do hope for help. Thanks

    
asked by anonymous 30.06.2015 / 20:52

1 answer

0

The Camera.getPicture () function returns a base64 from a photograph taken.

link

Basically it's a giant string and it can be saved and you later display it in an Html or send it to a webservice, for example. For the tools you are working on I suggest that you work with base64 and save it, when you retrieve base64 string you can display in Html in this way.

<img src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoV2luZG93cykiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6MEVBMTczNDg3QzA5MTFFNjk3ODM5NjQyRjE2RjA3QTkiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6MEVBMTczNDk3QzA5MTFFNjk3ODM5NjQyRjE2RjA3QTkiPiA8eG1wTU06RGVyaXZlZEZyb20gc3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