将图像推入阵列

时间:2016-06-07 08:55:03

标签: javascript arrays angularjs ionic-framework

我想为一个项目拍摄不同项目或多张照片的照片,但我正在努力弄清楚如何将图像推送到数组中。

从第一行显示单个图像:

<img ng-show="imgURI !== undefined" ng-src="{{imgURI}}" style="text-align: center">

但使用ng-repeat时,它不会显示一张图片。

我认为我的问题是:

  1. 我的ng-repeat有什么问题?
  2. 是否正确存储在数组内部的图像?
  3. HTML

     <button class="button button-full button-assertive" ng-click="takePhoto()">
        Take Photo
        </button>
        <img ng-show="imgURI !== undefined" ng-src="{{imgURI}}" style="text-align: center">
    <ion-item class="item item-assertive">Test array</ion-item>
     <div ng-repeat="x in array">
         <img ng-src="{{x.imgURI}}">
     </div> center">
    

    的Javascript

      $scope.takePhoto = function () {
                  var options = {
                    quality: 75,
                    destinationType: Camera.DestinationType.DATA_URL,
                    sourceType: Camera.PictureSourceType.CAMERA,
                    allowEdit: true,
                    encodingType: Camera.EncodingType.JPEG,
                    targetWidth: 300,
                    targetHeight: 300,
                    popoverOptions: CameraPopoverOptions,
                    saveToPhotoAlbum: false
                };
    
                    $scope.array = [];
    
                    $cordovaCamera.getPicture(options).then(function (imageData) {
                        $scope.imgURI = "data:image/jpeg;base64," + imageData;
                        $scope.imgURI = array;
                    }, function (err) {
                        // An error occured. Show a message to the user
                    });
                }
    

2 个答案:

答案 0 :(得分:2)

您正在将imageUri设置为数组的值。

$scope.imgURI = ar;

我认为你想像这样推动阵列:

$scope.array.push($scope.imgURI);

答案 1 :(得分:0)

测试这段代码,我在takePhoto函数之外创建了数组并将图像推入其中。

$scope.array = [];
$scope.takePhoto = function () {
    var options = {
        quality: 75,
        destinationType: Camera.DestinationType.DATA_URL,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 300,
        targetHeight: 300,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false
    };



    $cordovaCamera.getPicture(options).then(function (imageData) {
        $scope.array.push("data:image/jpeg;base64," + imageData);
    }, function (err) {
    // An error occured. Show a message to the user
    });
}

你的html应该如下所示

<div ng-repeat="x in array"> <img ng-show="x !== undefined" ng-src="{{x}}"> </div>`