Ion-scroll blurs my image when zooming in in ionic

2019-06-22 01:23发布

问题:

I load a picture within the "ion-scroll" tag in ionic framework. When I then try to zoom in, the picture is blurred and the letters are unreadable. This happens both in my browser as well as on android.

The code for my template:

<ion-view view-title="{{map.name}}" ng-controller="MapsCtrl">

<ion-scroll zooming="true"
        direction="xy" 
        delegate-handle="zoom-pane" 
        min-zoom="1" 
        max-zoom="20" 
        scrollbar-x="false" 
        scrollbar-y="false" 
        overflow-scroll="false">
    <img style="width:100%; heigth:100%" ng-src="{{map.img}}"/>

</ion-scroll>   

The picture I use is 4642 x 4642 pixels, so the image should be sharp when zoomed in.

回答1:

Browsers do this for performance optimization, there's no need to keep the hi-res version of the image when the page is rendered.

I was able to get around this by loading the image in full size then using the $ionicScrollDelegate handle to display the correctly zoomed. It isn't very elegant but it works in iOS and Android.

Template

<ion-scroll
    id="imgContainer"
    max-zoom="10.0" min-zoom="0.10"
    zooming="true" direction="xy"
    style="max-width:100%; height:100vh; width:100vh;"
    overflow-scroll="false"
    delegate-handle="imgContainer">
    <img ng-src="{{imageUrl}}" />
</ion-scroll>

Controller

.controller('imageCtrl', function ($ionicPlatform, $ionicScrollDelegate, $scope, $state, $http, $stateParams)
{
    //Create DOM img element
    var tmpImg = document.createElement('img');
    tmpImg.src = 'assets/images/hi_res_image.svg';

    //Ensure image loads
    var imgLoadPoll = setInterval(function () {
        if (tmpImg.naturalWidth) {
            clearInterval(imgLoadPoll);

            //Full img dimensions can be used in scope
            $scope.imageWidth = tmpImg.naturalWidth;
            $scope.imageHeight = tmpImg.naturalHeight;

            //Calculate Zoom Ratio
            var imgContainerWidth = document.getElementById('imgContainer').clientWidth;
            var zoomRatio = ( (imgContainerWidth) / tmpImg.naturalWidth);

            //Load Image & Animate Zoom
            console.log('Loaded: image (' + tmpImg.naturalWidth + 'px wide) into container (' + imgContainerWidth + 'px wide) requiring zoom: ' + zoomRatio);
            $scope.imageUrl =  'assets/images/hi_res_image.svg';
            $ionicScrollDelegate.$getByHandle('imgContainer').zoomTo(zoomRatio,1,0,0); //Set 1->0 to disable animation
        }
    }, 10);
}