简体   繁体   English

如何在AngularJS中旋转base64图像

[英]How can I rotate a base64 image in AngularJS

I have a json source that contains base64 images, and I would like to display them all in portrait. 我有一个包含base64图像的json源,我想将它们全部以纵向显示。 I think I need to build an array of Image objects, and rotate them as I build the array, but I'm not sure how to display the image objects in the view. 我想我需要构建一个Image对象数组,并在构建该数组时旋转它们,但是我不确定如何在视图中显示图像对象。

This is the array I've built 这是我建立的数组

angular.forEach($scope.trayDetails.tray_images, function(trayImage) {
    var myBase64 = "data:image/png;base64,"+trayImage.data;
    var img = new Image();
    img.src = myBase64;
    if (img.width>img.height ) {
        $(img).css({
            'transform': "rotate(90deg)"
        });
    }
    $scope.savedPictures.push(img);
});

But now I'm not sure what to do with the array, I tried these 2 methods with no luck 但是现在我不确定如何处理数组,我没有运气尝试了这两种方法

<li ng-repeat="savedPicture in savedPictures" class="photo">
    {{savedPicture}}
</li>

<li ng-repeat="savedPicture in savedPictures" class="photo">
    <img ng-src="{{savedPicture}}" />
</li>

The solution is easier than you think: 该解决方案比您想象的要容易:

angular.forEach($scope.trayDetails.tray_images, function(trayImage) {
   var myBase64 = "data:image/png;base64,"+trayImage.data;
   var img = new Image();
   img.src = myBase64;
   img.isRotated = img.width > img.height; //Use css class to rotate
   $scope.savedPictures.push(img);
});

Then add the img into html,: 然后将img添加到html中:

<li ng-repeat="savedPicture in savedPictures" class="photo">
     <img ng-class="{rotateImg: savedPicture.isRotated}" ng-src="{{savedPicture.src}}" />
</li>

The img tags will have rotateImg class if its width is greater than its height. 如果img标签的宽度大于其高度,则它将具有rotateImg类。 The css for rotateImg class is: rotateImg类的CSS是:

.rotateImg : { transform: rotate(90deg); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM