PDA

View Full Version : Performance issue with UIImage in Table View




CorporateFelon
Dec 3, 2009, 10:15 AM
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



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
Dec 3, 2009, 10:24 AM
It'll be redownloaded everytime the row is visible. You should cached the image instead.

CorporateFelon
Dec 3, 2009, 10:26 AM
It'll be redownloaded everytime the row is visible. You should cached the image instead.

That was my thought, any idea or pointers on how I would go about caching locally? Or just something to point me in the right direction.

namanhams
Dec 3, 2009, 10:34 AM
Something like this :

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

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

PhoneyDeveloper
Dec 3, 2009, 11:21 AM
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
Dec 4, 2009, 08:34 AM
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.