2

I am loading the image to the tableView, each time function

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

is executed, different url of image will come..

My problem is its taking so much time to load the image..

My code is..

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.tableView1 = tableView;
    static NSString *simpleTableIdentifier = @"SimpleTableCell";
    SimpleTableCell *cell = (SimpleTableCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

    cell.nameLabel.text = title;

    NSString *imageURL = [NSString stringWithFormat: @"http://www.xyz.com/image1.png];

     cell.thumbnailImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:fullURL]]];
}
return cell;
}

every time the image url will change and it will take time to load each image..

can any one suggest any idea to solve this problem? how multithreading will work with this code? where and what should i edit in the code?

Raju
  • 441
  • 1
  • 6
  • 17

4 Answers4

3

You have to download image in background like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *simpleTableIdentifier = @"SimpleTableCell";
    SimpleTableCell *cell = (SimpleTableCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier];   
         dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
        dispatch_async(queue, ^{

            NSString *u=[NSString stringWithContentsOfFile:r.url encoding:NSUTF8StringEncoding error:nil];
            NSURL *imageURL=[NSURL URLWithString:u];
            NSData *imageData=[NSData dataWithContentsOfURL:imageURL];
            dispatch_sync(dispatch_get_main_queue(), ^{
                SimpleTableCell *cell = (SimpleTableCell *)[tableView cellForRowAtIndexPath:indexPath];
                 cell.thumbnailImageView.image=[UIImage imageWithData:imageData];
                [cell setNeedsLayout];

            });
        });

    }

    return cell;
}

Just use above code in your table view method with required edit this should work for you.

V-Xtreme
  • 7,230
  • 9
  • 39
  • 79
  • hi thanks.. i want to know what is r.url is here.. and im not loading the URL from any file.. these url r dynamic and im accessing it from net.. – Raju Mar 07 '13 at 12:19
  • @Raju:Actually in my application i was showing the url in the cell . You don't required to do it .I have edited my answer.Is it working for image ? – V-Xtreme Mar 07 '13 at 12:30
  • Thank you so much Dude...its really lots of help me – Ankur Kathiriya Jul 15 '15 at 12:04
1

You don't need multi threading. At least you don't need to do that on your own.

Have a look at this tutorial. It was very helpful for me when I started with Apps. http://www.markj.net/iphone-asynchronous-table-image/

Alternatively subclass UIImageView and do this: http://codeshape.wordpress.com/2011/05/10/creating-a-lazy-loading-uiimageview-for-ios/

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
0

Take a look at the SDWebImage framework:

SDWebImage Framework

Maybe it'll help you.

Jeremy
  • 1,461
  • 12
  • 26
0

An alternative to Jeremy's answer which i have used before is the HJCache framework

Can just use the HJManagedImage object, it will download and cache the image asynchronously for you

Fonix
  • 11,447
  • 3
  • 45
  • 74