2014-05-23 18 views
0

以下代碼大部分工作正常,但有時在更改爲blob後,圖像視圖不顯示圖像。將圖像視圖變爲blob並不總是有效.toImage();

//store in temp image view, convert to blob 
       imageViewTemp.image = "imagename.jpg" 

       blob = imageViewTemp.toBlob(); 


       albumTitle = data[x].name + ' (' + numberPhotos + ')'; 

       var row = Titanium.UI.createTableViewRow({ 
        titleAlb : data[x].name, 
        width : '100%', 
        height : 'auto' 
       }); 
       var image = Titanium.UI.createImageView({ 
        top : 0, 
        left : 0, 
        width : '75', 
        height : '75' 
       }); 
       var title = Titanium.UI.createLabel({ 
        text : albumTitle, 
        top : 0, 
        left : 110, 
        width : 'auto', 
        height : 'auto' 
       }); 



       var width = blob.width; 
       var height = blob.height; 

       //crop so it fits in image view 
       if (width > height) { 


        image.image = ImageFactory.imageAsCropped(blob, { 
         width : height, 
         height : height, 
         x : 60, 
         y : 0 
        }); 

       } else { 


        image.image = ImageFactory.imageAsCropped(blob, { 
         width : (width - 1), 
         height : (width - 1), 
         x : 60, 
         y : 0 
        }); 

       } 



       row.add(image); 
       row.add(title); 
       rows.push(row); 


      } 

爲了改變圖像的尺寸,我使用了一個名爲image factory的模塊。之前,我可以改變它,我必須存儲tempory圖像視圖內的圖像,然後我轉換成二進制大對象:

   blob = imageViewTemp.toBlob(); 

問題是屏幕有時呈現後,這將不起作用:

image.image = ImageFactory.imageAsCropped(blob, { 
          width : height, 
          height : height, 
          x : 60, 
          y : 0 

}); 

Othertimes it will。

我在網上閱讀,這個問題可能與佈局後周期,但我不知道,或者如何進行

http://developer.appcelerator.com/question/174329/what-are-the-difference-between-toblob--toimage

感謝所有幫助。

回答

0

解決了這個問題。

在將文件放入臨時圖像視圖之前,您必須首先使用REST API方法(GET)下載圖像,否則將在文件完全下載之前呈現臨時圖像視圖(.toImage是異步調用回來的方法),讓一個無用的blob和沒有圖像的方式。

此方法的唯一問題是您依賴於您的REST API調用不會失敗。