iOS Performance issue with UIImage in Table View

CorporateFelon

macrumors regular
Original poster
Oct 26, 2007
177
0
Boston, MA
So I have an app that looks similar to the Youtube app. A Table view and in each cell an Image with text describing the record.

However, after launching the app I've received complaints about the performance when scrolling through the list in the Table View. I hadn't seem this during testing or development so I didn't know it existed, but that's my fault for not properly testing as much as I could.

Any way, to populate the images I am pulling them from the internet using the follow code with in the cellForRowAtIndexPath method

Code:
UIImage *uiThumbImage;

if(itemobj.image != nil)
{
	NSURL *imageUrl = [NSURL URLWithString: itemobj.image];
	NSData *imageData = [NSData dataWithContentsOfURL: imageUrl];
	uiThumbImage = [[UIImage alloc] initWithData:imageData];
}
else
{
	uiThumbImage = [UIImage imageName: @"DefaultThumb.png"];
}

[[cell uiImage] setImage:uiThumbImage];
I am recycling the TableViewCell with dequeueReusableCellWithIdentifier so my understanding is that the code will run every time the cell is coming back on screen. However now that I think about it here is my question. Will the program try to dl the images every time the cell is displayed again? Or are they cached some where? Even though the images are small I think if they have to be downloaded again every time the user scrolls through the records that may be the cause of my performance issues.

If this is the case where they images are being redownloaded every time instead of being cached, what would be an efficient way to go about caching that images?
 

namanhams

macrumors regular
Jun 3, 2009
153
0
It'll be redownloaded everytime the row is visible. You should cached the image instead.
 

namanhams

macrumors regular
Jun 3, 2009
153
0
Something like this :

Code:
cellForRowAtIndexPath  :
      if (images[row] == nil)
      {
           download the image and put it inside images[row];
      }

      [[cell uiImage] setImage:images[row]];
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
You can't use a synchronous method for getting the images. What happens if the net is slow, or unavailable?

You need to use the NSURLConnection asynchronous method for downloading the images. You need to cache them locally, either in memory or on disk. You need to show a blank image if the real image hasn't yet been downloaded and then update the table when the real image becomes available.

Also, look at this

http://developer.apple.com/iphone/library/samplecode/LazyTableImages/index.html
 

CorporateFelon

macrumors regular
Original poster
Oct 26, 2007
177
0
Boston, MA
You can't use a synchronous method for getting the images. What happens if the net is slow, or unavailable?

You need to use the NSURLConnection asynchronous method for downloading the images. You need to cache them locally, either in memory or on disk. You need to show a blank image if the real image hasn't yet been downloaded and then update the table when the real image becomes available.

Also, look at this

http://developer.apple.com/iphone/library/samplecode/LazyTableImages/index.html
Thanks for the link to an example, that was a perfect guide for fixing my issues.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.