【问题标题】:Cache UIImage using uitableview from url使用来自 url 的 uitableview 缓存 UIImage
【发布时间】:2014-04-22 09:34:17
【问题描述】:

我有一个 uitableview ,其中包含两个 UImage 的自定义单元格。徽标图像取自在线网站,这就是需要缓存图像的原因。加载图像到现在是这样的:

 NSURL * imageURL = [NSURL URLWithString:[arra1 objectAtIndex:indexPath.row / 2]];
 NSData * imageData = [NSData dataWithContentsOfURL:imageURL];

 NSURL * imageURL2 = [NSURL URLWithString:[arra2 objectAtIndex:indexPath.row / 2]];
 NSData * imageData2 = [NSData dataWithContentsOfURL:imageURL2];

 cell.ima1.image = [UIImage imageWithData:imageData];
 cell.ima2.image2 = [UIImage imageWithData:imageData2];

我从搜索中了解到,dataWithContentsOfURL 不是异步的,并且在滚动时会花费很多时间。我尝试了几种方法,但我似乎无法找到正确的一种。这是我第一次缓存 UIImages ,我非常感谢详细的实现解释,这样我可以在完成工作的同时学习。 非常感谢

【问题讨论】:

  • 缓存 UIImage 使用 SDWebImage github.com/rs/SDWebImage
  • @JayGajjar 谢谢你的回复,我试过但我迷路了,我真的很新,你能告诉我一个实现吗?
  • 很简单,只需将 setImageWithURL 与您的 uiimageview 一起使用。其余部分将由 sdk 管理。
  • 我试过了,@JayGajjar 但 uitableview 滚动仍然非常糟糕而且非常慢..

标签: ios objective-c uitableview caching uiimage


【解决方案1】:

我使用这个库,非常完美

您只需要#import <SDWebImage/UIImageView+WebCache.h> 到您的项目,并且您还可以在下载图像时使用以下代码定义占位符:

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

它还缓存下载的图像并为您提供出色的性能。

希望对你有帮助!

【讨论】:

  • 我感激不尽!!在我尝试 SDWebImage 之前,但我未能正确实现它,所以结果很混乱.. 如果使用 placeHolderImage,此方法的秘密!永远不要删除它,这就是我整天尝试的内容与@Erid 建议的内容之间的区别……谢谢,这是供所有未来的开发人员考虑的,……
  • 很高兴它对您有所帮助! :)
【解决方案2】:

SDWebImage,在我看来,是最好的选择。

您只需将其包含在您的应用中并像这样使用它:

  • SDWebImageManager *manager = [SDWebImageManager sharedManager];
    
    [manager downloadWithURL:[NSURL URLWithString:image_url]
                     options:0
                    progress:nil
                   completed:^(UIImage *images, NSError *error, SDImageCacheType cacheType, BOOL complete) {
    
                       myImageView.image = images;
                   }] ;
    

它异步下载图片,所以不会阻塞用户界面。

【讨论】:

  • 使用它仍然可以保持 tableview 并且仍然需要很长时间才能滚动:S 我错过了什么
  • cellForRowAtIndexPath 我应该将它添加到其他地方吗?
【解决方案3】:

您可以查看这些示例应用程序

  1. LazyTableImages - Apple 的示例应用程序
  2. MonoTouch-LazyTableImages
  3. robertmryan- LazyTableImages - 清楚地解释了苹果示例应用程序的限制。

希望这会有所帮助。

【讨论】:

    【解决方案4】:

    结帐 UIImageLoader https://github.com/gngrwzrd/UIImageLoader

    易于加载图像,并且您可以为您想要处理的所有场景获得回调:

    NSURL * imageURL = myURL;   
    
    [[UIImageLoader defaultLoader] loadImageWithURL:imageURL \
    
    hasCache:^(UIImage *image, UIImageLoadSource loadedFromSource) {
    
        //there was a cached image available. use that.
        self.imageView.image = image;
    
    } sendRequest:^(BOOL didHaveCachedImage) {
    
        //a request is being made for the image.
    
        if(!didHaveCachedImage) {
            //there was not a cached image available, set a placeholder or do nothing.
    
            self.loader.hidden = FALSE;
            [self.loader startAnimating];
    
            self.imageView.image = [UIImage imageNamed:@"placeholder"];
        }
    
    } requestCompleted:^(NSError *error, UIImage *image, UIImageLoadSource loadedFromSource) {
    
        //network request finished.
    
        [self.loader stopAnimating];
        self.loader.hidden = TRUE;
    
        if(loadedFromSource == UIImageLoadSourceNetworkToDisk) {
            //the image was downloaded and saved to disk.
            //since it was downloaded it has been updated since
            //last cached version, or is brand new
    
            self.imageView.image = image;
        }
    }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      相关资源
      最近更新 更多