在表视图中的图像延迟加载

时间:2012-09-05 09:03:07

标签: iphone objective-c xcode

我想在我的表视图中使用延迟加载图像概念。我的图像rul字符串数组是(MSMutableArray)“images”。如何在cellForRowAtIndexPath中实现此功能。

看起来像:

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

    UITableViewCell *cell=[searchtable dequeueReusableCellWithIdentifier:@"cell"];

    if(!cell)
    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];



        cell.textLabel.font=[UIFont systemFontOfSize:12];

        cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[appSmallLogos objectAtIndex:indexPath.row]]]];

        cell.detailTextLabel.text=[appReleaseDates objectAtIndex:indexPath.row];


    }

    return cell;
}

3 个答案:

答案 0 :(得分:0)

您可以使用SDWebImage库,库将以异步方式下载和缓存图像。将url作为参数传递给它

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier] autorelease];
    }

    // Here we use the new provided setImageWithURL: method to load the web image
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    cell.textLabel.text = @"My Text";
    return cell;
}

答案 1 :(得分:0)

试试这段代码:

   UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.frame = CGRectMake(your bounds);  
[cell addSubview:indicator];
[indicator bringSubviewToFront:self.view];
[indicator startAnimating];


        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
    dispatch_async(queue, ^{
     //This is what you will load lazily
        NSString *u=[NSString stringWithContentsOfFile:r.url encoding:NSUTF8StringEncoding error:nil];
        NSURL *imageURL=url;
        NSData *image=[NSData dataWithContentsOfURL:imageURL];
        dispatch_sync(dispatch_get_main_queue(), ^{
            CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
            cell.imageView.image=[UIImage imageWithData:image];
            [cell setNeedsLayout];
  [indicator stopAnimating];

        });
    });
cell.cellDescription.text=r.url;// this is which you want to add in first time.

我不确定但是试试。

答案 2 :(得分:0)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    TableCellLayout *cell = (TableCellLayout *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[TableCellLayout alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.cellName = [[array valueForKey:@"name"] objectAtIndex:indexPath.row];
    cell.cellImage = [UIImage imageNamed:@"placeholder.png"]; // add a placeholder    

    NSString *imageURL = [[array valueForKey:@"imageLink"] objectAtIndex:indexPath.row];
    NSURL *theURL = [NSURL URLWithString:imageURL];

    if (asynchronousImageLoader == nil){
        asynchronousImageLoader = [[AsynchronousImages alloc] init];
    }
    [asynchronousImageLoader loadImageFromURL:theURL];

    cell.cellImage = asynchronousImageLoader.image;

return cell;
}

我终于设法用以下方式设置图像:

– performSelectorOnMainThread:withObject:waitUntilDone:

下载图片后