显示从url下载图像并在customtablecell中显示所花费的时间

时间:2015-10-27 06:44:14

标签: ios objective-c uitableview

这是我的代码,请帮忙。在下面我需要花时间从URL加载图像并在自定义tableview单元格中显示时间。我们可以使用NSTimer或NSDate。

提前致谢。

- (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;
}

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码。但为此你必须使用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;
}

下载SDWebImage Use this link

答案 1 :(得分:0)

.m文件中定义以下方法

- (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);
        }
    }];
}

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];
        }
    }];
}