1

Here is my code, please help. In the below I need to get the time taken to load image from url and display the time in custom tableview cell. Which we can use either NSTimer or NSDate.

Thanks in advance.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";
    TableViewCell *cell = (TableViewCell *)[_tableViewUsername dequeueReusableCellWithIdentifier:CellIdentifier];
    NSDate *object = arrURL[indexPath.row];
    rowCount = indexPath.row;
    cell.userName.text = [arrUserNames objectAtIndex:indexPath.row];

    if ([object valueForKey:@"status"])
    {
        if([[object valueForKey:@"status"]isEqualToString:@"completed"] && [object valueForKey: @"image"]  && [[object valueForKey: @"image"] isKindOfClass:[UIImage class]])
        {
            cell.customImageView.contentMode = UIViewContentModeScaleToFill;
            cell.customImageView.image = [object valueForKey:@"image" ];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
    else
    {
        [object setValue:@"inprogress" forKey:@"status"];
        [self.operationQueue addOperationWithBlock:^
        {
            UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
                                                      [NSURL URLWithString:[object valueForKey:@"url"]]]];
            [object setValue:image forKey:@"image"];
            [object setValue:@"completed" forKey:@"status"];
            //count set
            [[NSOperationQueue mainQueue] addOperationWithBlock:^
            {
            [_tableViewUsername reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
            }];
        }];

    }

    return cell;
}
Naveen
  • 19
  • 1
  • Do you want to display the timing in your custom cell that denotes that image will be downloaded and displayed within this time period? Like decrementing the timing? – Vijay Oct 27 '15 at 06:52
  • Yes I just want to check how much time it is taking to download the image and need to display that time in custom tableview cell – Naveen Oct 27 '15 at 09:49
  • Possibly dublicate http://stackoverflow.com/questions/15433956/showing-accurate-progress-in-uiprogressview-while-downloading-images-in-iphone – Vijay Oct 27 '15 at 11:28

2 Answers2

0

You can use following code. But for this you have to use SDWebImageCache

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
  __block  NSDate *startTime = [NSDate date];
    [cell.nImageView sd_setImageWithURL:[NSURL URLWithString:[imageUrlArray objectAtIndex:indexPath.row]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    NSDate *endTime = [NSDate date];
    NSTimeInterval secs = [endTime timeIntervalSinceDate:startTime];
    NSLog(@"Seconds (%ld) --------> %f",(long)indexPath.row, secs);
}];

return cell;
}

To download SDWebImage Use this link

KavyaKavita
  • 1,581
  • 9
  • 16
0

Define below method in your .m file

- (void)downloadImageFrom:(NSString *)strURL response:(void (^)(NSData *data))handler
{
    strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strURL]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        if (data)
        {
            handler(data);
        }
    }];
}

And call from cellForRowAtIndexpath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";
    TableViewCell *cell = (TableViewCell *)[_tableViewUsername dequeueReusableCellWithIdentifier:CellIdentifier];

    NSDate *object = arrURL[indexPath.row];
    rowCount = indexPath.row;

    cell.userName.text = [arrUserNames objectAtIndex:indexPath.row];
    cell.customImageView.contentMode = UIViewContentModeScaleToFill;
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    [self downloadImageFrom:[object valueForKey:@"url"] response:^(NSData *data) {

        if (data)
        {
            cell.customImageView.image = [[UIImage alloc] initWithData:data];
        }
    }];
}
VRAwesome
  • 4,721
  • 5
  • 27
  • 52