【问题标题】:How to cache images properly in ios如何在ios中正确缓存图像
【发布时间】:2016-07-26 12:37:42
【问题描述】:

我正在使用SDWebImage 来缓存图像。但它不起作用。我在cellForRowAtIndexPath下以这种方式使用它:

[cell.image sd_setImageWithURL:[NSURL URLWithString:[NSMutableString stringWithFormat:@"%@app/media/access/pictures?p=%@",baseurl,data.picPath]]
                 placeholderImage:[UIImage sd_animatedGIFNamed:@"image_loader.gif"] options:SDWebImageRefreshCached];

我想它是异步下载图像的。但是我的应用在滚动UITableView 时冻结。并且在向上滚动时也有网络使用,对我来说这不应该发生,因为我正在缓存它。

除了我有一些逻辑可以根据heightForRowAtIndexPath 下的图像大小调整UIImageView 框架的大小,这可能是什么原因???

if(data.type == 2)
    {
        height = 260.00;
    }
    else if(data.type == 1)
    {
        height = self.view.frame.size.width/2 + 115;
    }
    else
    {
        NSString *filePath = [NSString stringWithFormat:@"%@app/media/access/pictures?p=%@",baseurl,data.picPath];
        self.img = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]]];


        if (self.img.size.width > CGRectGetWidth(self.view.bounds)) {
            CGFloat ratio = self.img.size.height / self.img.size.width;
            return CGRectGetWidth(self.view.bounds) * ratio+120;
        } else {
            return self.img.size.height+110;
        }




    }

【问题讨论】:

  • 查看已编辑的答案@Fay007

标签: ios uitableview caching sdwebimage


【解决方案1】:

这是肯定的:

self.img = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]]];

为什么要重新下载图片?您应该始终使用SDWebImage

添加 而不是做

NSString *filePath = [NSString stringWithFormat:@"%@app/media/access/pictures?p=%@",baseurl,data.picPath];
self.img = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]]];

您必须先下载图像,因为heightForRow 不会等待异步块。尝试使用SDWebImage sharedDownloader 预加载它们。然后它们将在缓存中。

【讨论】:

  • tnx 的答案,但如何调用 SDWebImage 而不是 [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]]];
【解决方案2】:

heightForRowAtIndexPath 中的以下代码导致问题:

NSString *filePath = [NSString stringWithFormat:@"%@app/media/access/pictures?p=%@",baseurl,data.picPath];
        self.img = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]]];

每次滚动tableView时都会执行代码,由于它不是异步调用,因此会导致冻结问题。

编辑: 为避免这种情况,您可以执行以下操作:

下载完成后就可以使用了,

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] 
                 withRowAnimation:UITableViewRowAnimationAutomatic];

这会再次调用

    -(CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath

喜欢,

[cell.image sd_setImageWithURL:[NSURL URLWithString:[NSMutableString stringWithFormat:@"%@app/media/access/pictures?p=%@",baseurl,data.picPath]]

                                     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

                                         if (error) {

                                             NSLog(@"error: %@",error.description);


                                         }
                                         else{

                                             //get image height and return this height from HeightForRowAtIndexPath
                                             Height=image.size.height;

                                             //reloading row will call heightForRowAtIndexPath,where now you can return height got above
                                             [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];

                                             NSLog(@"Not an error in image download");
                                         }




                                     }];

编辑1

要检查已经缓存的图像,可以使用 SDWebImage 库的以下方法:

- (BOOL)diskImageExistsWithKey:(NSString *)key;

缓存键是要缓存的图像的应用程序唯一标识符。一般是图片的绝对网址。

如果缓存中存在图片,则使用SDWebImage库的以下方法获取图片路径:

- (NSString *)defaultCachePathForKey:(NSString *)key;
- (NSString *)cachedFileNameForKey:(NSString *)key;

您可以像这样获取图像的路径:

NSString *ImageKey;//key for the image you want the path for
[[SDImageCache sharedImageCache] defaultCachePathForKey:ImageKey];

如果你不使用默认位置,你可以使用:

- (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path;

更多详情请点击此处:SDWebImage

【讨论】:

  • 是的,我理解逻辑......但是如何检查图像是否已经被缓存,这样我就不必再次下载了
  • 太棒了!享受编码@Fay007
猜你喜欢
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多