2016-07-26 98 views
1

我正在將很多圖像加載到鈦圖像視圖中。問題是,在Android我得到內存問題。這是我的功能將圖像放入圖像視圖。在這個功能我正在調整圖像。將很多圖像加載到鈦圖像視圖

exports.SetImg = function(img, hightFactor, widthFactor, viewObj, deviceWidth, deviceHeight) { 


    if (img != null && img != "") { 
     var IMGhight = 0, 
      IMGwidth = 0, 
      height = 0, 
      width = 0; 

     var imgTemp = Ti.UI.createImageView({ 
      image : img 
     }); 
     if (imgTemp != null) { 

      if (imgTemp.toBlob().height != null && imgTemp.toBlob().width != null) { 


       IMGhight = imgTemp.toBlob().height; 
       IMGwidth = imgTemp.toBlob().width; 
       height = alturaImg; 
       width = larguraImg; 

       if (height > deviceHeight * hightFactor) { 
        if (height > deviceHeight * hightFactor) { 
         height = deviceHeight * hightFactor; 
         width = (((deviceHeight * hightFactor)/IMGhight) * IMGwidth); 
        } 

        if (width < deviceWidth * widthFactor) { 
         width = deviceWidth * widthFactor; 
         height = (((deviceWidth * widthFactor)/IMGwidth) * IMGhight); 
        } 
       } else if (width > deviceWidth * widthFactor) { 
        if (width > deviceWidth * widthFactor) { 
         width = deviceWidth * widthFactor; 
         height = (((deviceWidth * widthFactor)/IMGwidth) * IMGhight); 
        } 

        if (height < deviceHeight * hightFactor) { 
         height = deviceHeight * hightFactor; 
         width = (((deviceHeight * hightFactor)/IMGhight) * IMGwidth); 
        } 
       } else if (height < deviceHeight * hightFactor) { 
        if (height < deviceHeight * hightFactor) { 
         height = deviceHeight * hightFactor; 
         width = (((deviceHeight * hightFactor)/IMGhight) * IMGwidth); 
        } 

        if (width < deviceWidth * widthFactor) { 
         height = deviceWidth * widthFactor; 
         height = (((deviceWidth * widthFactor)/IMGwidth) * IMGhight); 
        } 
       } else if (width < deviceWidth * widthFactor) { 
        if (width < deviceWidth * widthFactor) { 
         width = deviceWidth * widthFactor; 
         height = (((deviceWidth * widthFactor)/IMGwidth) * IMGhight); 
        } 
        if (hight < deviceHeight * hightFactor) { 
         hight = deviceHeight * hightFactor; 
         width = (((deviceHeight * hightFactor)/IMGhight) * IMGwidth); 
        } 
       } 

       var imagem = Ti.UI.createImageView({ 
        width : width, 
        height : hight, 
        image : img 
       }); 

       viewObj.add(imagem); 
       imagem = null; 
       imgTemp = null; 
       IMhight = null; 
       IMGwidth = null; 
       hight = null; 
       width = null; 
       img = null; 
       hightFactor = null; 
       widthFactor = null; 
       viewObj = null; 
       deviceWidth = null; 
       deviceHeight = null; 
      } 
     } 
    } 
}; 

我已經將所有變量聲明爲null,但對於GC仍然存在內存問題。現在做一些如何解決這個問題?

This is the layout that I need do make.

的問題是,我需要重新使用別的東西相同的圖像,所以我需要他們的蜜蜂求。

+0

儘管您正在調整圖像大小,但重量完全相同。所以我認爲你必須爲你的應用程序找到某種惰性加載解決方案。 –

+0

什麼是「很多」 –

+0

約30張圖片,大小爲1024 * 800 –

回答

1

你真的應該調整圖像,並將其保存到縮略圖文件。而不是調整圖像視圖的大小並在其中顯示整個圖像。這會爲你節省一些記憶。

而且要創建多個斑點imgTemp.toBlob()。運行一次該命令並重新使用blob變量。所有其他計算都一樣。這並不多,但是當你只計算一次並重新使用該值時,可以節省一些時間/空間。

但是,你需要調整圖像大小,或許還有一個定製的Android模塊,支持批量處理文件的整個文件夾或磁盤陣列調整如果那是你所需要的。

+0

我從服務器接收圖片,並從URL中獲取圖片。它是如何創建圖像數組的最佳方法?謝謝 –

2

提取imgTemp.toBlob()給公共變量將會對你的內存消耗急劇積極的影響。看看封面下面會發生什麼,您實際上多次將圖像加載到內存中。每次您撥打toBlob()!我看到代碼中有4種用法,加上實際的圖像視圖使用情況,30倍圖像,你有幾百個位圖副本被丟棄。

看到這裏的源代碼,更多的證據 -

在TiUIImageView.java: https://github.com/appcelerator/titanium_mobile/blob/e6a5f6c086a019dbdf810e225bb13c5c8d9115ac/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIImageView.java#L945

而且在TiBlob.java: https://github.com/appcelerator/titanium_mobile/blob/415bd6c66dcc55b1a59a59574f3babd3c3a84ede/android/titanium/src/java/org/appcelerator/titanium/TiBlob.java

0

如果從服務器接收圖像,那麼它的更好地把調整大小的圖像放在服務器上。服務器上的圖像分辨率可以是您在應用中創建ImageView的最大寬度*高度。現在

,如果你要在更大的ImageView一些其他的屏幕使用這些圖像(如顯示全屏圖像),那麼你就可以在服務器這兩種情況下有不同的文件夾。例如

  • 較小ImageView的,在創建服務器
  • 一個縮略圖文件夾更大的ImageView,創建命名爲圖片另一個文件夾。
  • 現在,假設實際的圖像在服務器是大小寬度= 1024,高度= 800的my_image.jpg,然後把保持寬高比相同與此路徑縮略圖/ my_image.jpg的300 * 234的縮略圖圖像和實際的圖像在此路徑圖片/ my_image。JPG

同樣,如果你真的想根據您的要求來調整圖像,然後有在Titanium Blob API like imageAsThumbnail

漂亮整潔的方法還有,如果你要加載的1024 * 800的30張圖像有一次,那麼您將不得不查看延遲加載方法,因爲如果您一次創建30個1024 * 800的ImageView,則無法保存設備的內存。

最後,如果你能提示我們想要設計什麼樣的用戶界面,那麼我們可以建議你有更好的方法來做,而不是調整圖像大小並對它們進行很多計算。

+0

我更新了我的問題。我reeded的鈦文件,他們說,懶加載只適用於IOS,但我需要它爲Android –

+0

你可以實現你自己的方式進行延遲加載,因爲可能有多種方式來做到這一點。正如我所說,簡單的模型或線框設計對我們來說會更好,幫助您更好地實現自己想要的功能。 –

+0

我在我需要創建的UI的問題上放置了一個圖像 –