-2
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[getImage objectAtIndex:indexPath.row]]];
cell.imageView.image = [UIImage imageWithData:imageData];

I am fetching images from a server and displaying them on a UITableView. I'm finding that the scrolling is very slow. How do I remove this?

wjl
  • 7,143
  • 1
  • 30
  • 49
Pintu Rajput
  • 621
  • 2
  • 8
  • 23
  • possible duplicate of [How to increase the speed of the scroll in the table view when images are being loaded in each cell?](http://stackoverflow.com/questions/823475/how-to-increase-the-speed-of-the-scroll-in-the-table-view-when-images-are-being) – Fogmeister Oct 16 '14 at 14:23

2 Answers2

0

That datasource method will be called frequently, even for recently displayed cells, as iOS wants to reduce the total number of cells in the tableview, so it will destroy cells that are no longer visible and ask the tablview datasource method to recreate them when they become visible.

If you are fetching from the server for every cell population call then it's bound to be slow (an understatement).

Implement some caching and populate your content from the cache.

Droppy
  • 9,691
  • 1
  • 20
  • 27
0

When you call this code:

[[NSData alloc] initWithContentsOfURL:...]

this is reaching out over the network to get data. This can take several seconds (or longer), whereas you want to maintain 60 frames per second drawing. This means that your tableView:cellForRowAtIndexPath: method should execute in under 1/60 seconds, or 16 milliseconds, so network calls are out of the question.

The appropriate way to write this code is to load data asynchronously (using Grand Central Dispatch or another mechanism), cache it either on disk or using NSCache, and then informing the table view that it should reload to display newly-available data.

wjl
  • 7,143
  • 1
  • 30
  • 49
  • How to use GCD to increase the speed. – Pintu Rajput Oct 17 '14 at 05:15
  • Mike Ash's post introducing Grand Central Dispatch is a good starting point. If you have any questions about how to use it, you should start a new question. https://www.mikeash.com/pyblog/friday-qa-2009-08-28-intro-to-grand-central-dispatch-part-i-basics-and-dispatch-queues.html – wjl Oct 17 '14 at 16:11