0

I want to download an image to my tableViewCell but I only want the image downloaded to be inside a cell who is VISIBLE in my tableview. I want my tableView to download image when the tableviewcell is visible and during that time to display a loading bar or something until the download is finished and then display that image to the corresponding tableviewcell

This is the code I use to display image to tableviewcell:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("ContentCell", forIndexPath: indexPath)

    let contentCell = cell as! ContentCell

    contentCell.thumbnail.kf_setImageWithURL(self.contentArray[indexPath.row].thumbnailUrl)
    print("row: \(indexPath.row)")


    return contentCell
}

What I get is all the image is download all at once. I know this because When I scroll down I find that all the image is instantly displayed without delay even though my logging method print() only print visible cell's row.

Am I implementing this wrong?

samitarmum
  • 83
  • 8
  • 1
    Are you talking about asynchronous Image loading – HariKrishnan.P Jun 18 '16 at 14:53
  • Can you elaborate your question? – samitarmum Jun 18 '16 at 14:57
  • 1
    1. download the image and store it in local database then call the path. 2. Asynchronou image loading methods using to load the image while scrolling the tableview. 3. downloaded the image store it in data and assigned to array , after that call the array to show the image – HariKrishnan.P Jun 18 '16 at 14:58
  • Oh yes I use no.2: async image downloading, I only want to download image who are visible kinda like reddit official app – samitarmum Jun 18 '16 at 15:00
  • 1
    like button push to download the image and visible always after downloading right? – HariKrishnan.P Jun 18 '16 at 15:02
  • No I dont use any button to download the image but yes the image should be visible always after downloading. I want the download to happen automatically without a push of a button – samitarmum Jun 18 '16 at 15:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115008/discussion-between-harikrishnan-p-and-samitarmum). – HariKrishnan.P Jun 18 '16 at 15:10
  • Possible duplicate of [Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling](http://stackoverflow.com/questions/16663618/async-image-loading-from-url-inside-a-uitableview-cell-image-changes-to-wrong) – lchamp Jun 18 '16 at 17:29

1 Answers1

0

you will achieve using asynchronous image loading

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

cell.poster.image = nil; // or cell.poster.image = [UIImage imageNamed:@"placeholder.png"];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myurl.com/%@.jpg", self.myJson[indexPath.row][@"movieId"]]];

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (data) {
        UIImage *image = [UIImage imageWithData:data];
        if (image) {
            dispatch_async(dispatch_get_main_queue(), ^{
                MyCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
                if (updateCell)
                    updateCell.poster.image = image;
            });
        }
    }
}];
[task resume];

return cell;

}

HariKrishnan.P
  • 1,204
  • 13
  • 23