【问题标题】:Load image to a tableView from URL iphone sdk从 URL iphone sdk 将图像加载到 tableView
【发布时间】:2011-09-27 06:36:21
【问题描述】:

我有 tableView,需要从 URL 加载图像。我有一个包含图像 URL 的数组,当页面加载时,我需要将所有图像加载到 tableview 中。请注意,不是来自单个 URL,而是具有具有不同 URL 的数组。我该如何实施?请帮忙

谢谢。

【问题讨论】:

标签: iphone url uitableview image-loading


【解决方案1】:

您可以使用 GCD 在后台线程中加载图像,如下所示:

//get a dispatch queue
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //this will start the image loading in bg
    dispatch_async(concurrentQueue, ^{        
        NSData *image = [[NSData alloc] initWithContentsOfURL:imageURL];

        //this will set the image when loading is finished
        dispatch_async(dispatch_get_main_queue(), ^{
            imageView.image = [UIImage imageWithData:image];
        });
    });

嗨。但是您可能需要添加一个 dispatch_release(concurrentQueue);确保没有泄漏。 – 弗兰克 8 月 25 日 19:43

【讨论】:

  • 嗨。但是您可能需要添加一个 dispatch_release(concurrentQueue);确保没有泄漏。
  • 边问:使用dispatch_async时需要@autorelease pool吗?
【解决方案2】:

当你想从网上下载图片时,你可以使用延迟加载

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //All you reusable cell implementation here.
   //Since your Images sizes differ. Keep a custom Imageview

    if(![imagesForCategories containsObject:indexPath])
    {    
        customImageView.image = [UIImage imageNamed:@"default-image.png"];
        NSMutableArray *arr = [[NSArray alloc] initWithObjects:[imageUrlArray objectAtIndex:indexPath.row],indexPath, nil];
        [self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr];
        [arr release];
    }
    return cell;
}

- (void) loadImageInBackground:(NSArray *)urlAndTagReference 
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSURL *imgUrl=[[NSURL alloc] initWithString:[urlAndTagReference objectAtIndex:0]];
    NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
    UIImage *img = [UIImage imageWithData:imgData];
    [imgUrl release]; 
    NSMutableArray *arr = [[NSMutableArray alloc ] initWithObjects:img,[urlAndTagReference objectAtIndex:1], nil  ];
    [self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
    [arr release];
    [pool release];
}

- (void) assignImageToImageView:(NSMutableArray *)imgAndTagReference
{
    [imagesForCategories addObject:[imgAndTagReference objectAtIndex:1]];
    UITableViewCell *cell = [celebCategoryTableView cellForRowAtIndexPath:[imgAndTagReference objectAtIndex:1]];
    UIImageView *profilePic = (UIImageView *)[cell.contentView viewWithTag:20];
    profilePic.image = [imgAndTagReference objectAtIndex:0];

}

【讨论】:

【解决方案3】:

您可以使用 SDWebImage,它允许在 UITableView 中轻松快速地加载图像。
https://github.com/rs/SDWebImage

【讨论】:

    【解决方案4】:

    试试这个代码,imagearray包含图片的url

    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [imagearray objectAtIndex:row]]];
        UIImage* image = [[UIImage alloc] initWithData:imageData];
    
    
        cell.imageView.image =image;
        return cell;
    }
    

    【讨论】:

    • 这将在图像下载时冻结界面
    • 谢谢,它工作正常。但图片大小不同。
    • 还有,跑得慢,有什么办法让它跑得快,有时会冻死。
    • 如果数据量大(图片)需要异步下载...查看此链接markj.net/iphone-asynchronous-table-image
    【解决方案5】:

    您需要为延迟加载创建自定义单元格。这将允许您正确下载图像而不会冻结。这是如何做到这一点的好例子: Asynch image loading

    【讨论】:

      【解决方案6】:

      有了afnetworki,太简单了。

      //afnetworking
      
      #import "UIImageView+AFNetworking.h"
      
       [cell.iboImageView setImageWithURL:[NSURL URLWithString:server.imagen] placeholderImage:[UIImage imageNamed:@"qhacer_logo.png"]];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 2011-05-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多