Problems with display.newImage()

I’m going to mention the word tableView, please do not move this to the widget group. I’m using widgets, but this isn’t a widget problem, maybe.

I’m trying to build a tableView of all of my facebook friends. I have it mostly working as long as I drag the tableView I don’t have problems. “Okay, so what’s wrong?” Well I have two specific problems, so lets focus on this one first:

In the working version of the code, I get the URL for the 50x50 avatar from Facebook like I expect and I fire off a:

display.loadRemoteImage( url, “GET”, thumbListener, filename, system.CachesDirectory, 0, 0 )

filename is the Facebook userID and a “.jpg”. My thumbListener fires after it’s done downloading and I get the image like expect.

However it’s stupid to download the images every time and when I try to rapidly jump through the tableView (via search or via one of those Contact like A, B, C bars) I get all kinds of errors about :insert being null on the tableView row group. So I decided that I would just load the existing files from cache. Consider this code:

 local lfs = require("lfs")  
  
local function onRowRender(event)  
 print("row render")  
 local row = event.target  
 local rowGroup = event.view  
 local id = event.index  
  
 print(id)  
 --  
 local function thumbListener( event )  
 if ( event.isError ) then  
 print ( "Network error - download failed" )  
 else  
 if shouldRenderImage then  
 --print ("got image")  
 --print\_r(event)  
 -- event.target is the loaded display.newImage from the downloaded image.  
 if event.target then  
 local w = event.target.width  
 local h = event.target.height  
 local s = 39 / h  
 event.target:scale(s,s)  
 event.target:setReferencePoint(display.TopLeftReferencePoint)  
 event.target.x = 200 - (w \* s)  
 event.target.y = 1  
 rowGroup:insert(event.target) -- this errors if I try to use list:scrollToIndex(index,0) saying it doesn't exist... some of the time.  
 else  
 local emptyPlayer = display.newImageRect("images/GS\_PlayerDefaultImage.png", 41, 39)  
 emptyPlayer:setReferencePoint(display.TopLeftReferencePoint)  
 emptyPlayer.x = 200 - 41  
 emptyPlayer.y = 1  
 rowGroup:insert(emptyPlayer)  
 end  
 end  
 end  
 --print ( "RESPONSE: " .. event.response )  
 end  
  
 local url = friends[id].avatar  
 local filename = friends[id].id .. ".jpg"  
 local file\_path = system.pathForFile( filename, system.CachesDirectory )  
  
 -- get last modified time  
 local file\_attr = lfs.attributes( file\_path )  
 local last\_mod  
  
 if file\_attr then  
 last\_mod = file\_attr.modification  
 print(file\_path,last\_mod,os.time(),(last\_mod + 24 \* 60 \* 60) \> os.time())  
 end  
 if file\_attr and (last\_mod + 24 \* 60 \* 60) \> os.time() then -- use cached file if it exists and is less than a day old  
 print("loading ", filename, "from", system.CachesDirectory)  
 local avatar = display.newImage(filename, system.CachesDirectory, 0, 0)   
 local w = avatar.width  
 local h = avatar.height  
 local s = 39 / h  
 print(w,h,s,avatar.x, avatar.y)  
 avatar:scale(s,s)  
 avatar:setReferencePoint(display.TopLeftReferencePoint)  
 avatar.x = 200 - (w \* s)  
 avatar.y = 1  
 rowGroup:insert(avatar)  
 else   
 if url then  
 display.loadRemoteImage( url, "GET", thumbListener, filename, system.CachesDirectory, 0, 0 )  
 else  
 local emptyPlayer = display.newImageRect("images/GS\_PlayerDefaultImage.png", 41, 39)  
 emptyPlayer:setReferencePoint(display.TopLeftReferencePoint)  
 emptyPlayer.x = 200 - 41  
 emptyPlayer.y = 1  
 rowGroup:insert(emptyPlayer)  
 end  
 end  

So to sum up, if I get to display.loadRemoteImage, the thumbListener fires and I get images (though some times if it’s too fast, I get errors.

I see the prints that tell me it should be loading the cached copy. The height and width are both 50 which I expect since that is the image size, so I know it’s actually loading the image, I just never get it displayed.

Can you see any reason why my cached images wouldn’t load?

Thanks
Rob
[import]uid: 19626 topic_id: 30775 reply_id: 330775[/import]