4

每次点击后,我都会尝试在 Android 手机上显示照片并将其旋转 90 度。它适用于较小的图像,但对于单击一段时间(例如大约 10 次)后 8 兆像素的照片,ctx.drawImage 停止绘制任何内容。它看起来像 Android WebView 中的一些内存泄漏,因为我手机上的 Chrome 可以很好地显示大图像。

WebView webview = (WebView) rootView.findViewById(R.id.webView1);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/ROTATING_image.html");

html代码是这样的:

<!DOCTYPE html>
<html>
<body>
    <canvas id="canvas"/>
</body>
<script type="text/javascript">
    (function(undefined) {
        var $ = function(id) {
            return document.getElementById(id);
        };
        var draw = function(degree) {
            var ctx = $('canvas').getContext('2d');

            var cw = img.width, ch = img.height, cx = 0, cy = 0;
            switch (degree) {
            case 90:
                cw = img.height;
                ch = img.width;
                cy = img.height * (-1);
                break;
            case 180:
                cx = img.width * (-1);
                cy = img.height * (-1);
                break;
            case 270:
                cw = img.height;
                ch = img.width;
                cx = img.width * (-1);
                break;
            }

            $('canvas').height = ch;
            $('canvas').width = cw;
            degree && ctx.rotate(degree * Math.PI / 180);
            ctx.drawImage(img,cx,cy);
        };
        var img = new Image;
            //Works fine with such small image, but not with the 8 megapixel one
        img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG';
        img.onload = function() {
            draw()
        };
        var curDegree = 0;
        $('canvas').onclick = function(e) {
            curDegree=curDegree+90;
            if(curDegree==360)
                {curDegree=0;}
            draw(curDegree);
        };
    })();
</script>
</html>
4

0 回答 0