1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Performance issue with UIImage in Table View

Discussion in 'iPhone/iPad Programming' started by CorporateFelon, Dec 3, 2009.

  1. macrumors regular


    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];
    	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?
  2. macrumors regular

    It'll be redownloaded everytime the row is visible. You should cached the image instead.
  3. macrumors regular


    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.
  4. macrumors regular

    Something like this :

    cellForRowAtIndexPath  :
          if (images[row] == nil)
               download the image and put it inside images[row];
          [[cell uiImage] setImage:images[row]];
  5. macrumors 68030


    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

  6. macrumors regular


    Thanks for the link to an example, that was a perfect guide for fixing my issues.

Share This Page