【问题标题】:Different cache types SDWebImage不同的缓存类型 SDWebImage
【发布时间】:2014-07-23 15:48:14
【问题描述】:

我使用 SDWebImage 缓存我的所有图像有一段时间了,但现在我想拥有多个缓存来对各种类型的图像进行分组。例如三种缓存,每种缓存有几个图像,所以在运行时我想清除其中一个或者有不同的setMaxCacheAge

示例: 图像类型 = 汽车图像是一种类型,摩托车是另一种类型……飞机其他……像这样。存储此图像后,我只想删除或清除摩托车图像的缓存(一种)

现在我有了这个,但它适用于缓存的每个图像:

SDImageCache * sDImageCache = [SDImageCache sharedImageCache];
[sDImageCache setMaxCacheAge:60*60*24];

...

SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache clearMemory];
[imageCache clearDisk];
[imageCache cleanDisk];

-

我看到了,但真的是我想要的吗?

SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"myNamespace"];

Using Asynchronous Image Caching Independently

【问题讨论】:

    标签: ios objective-c caching sdwebimage image-caching


    【解决方案1】:

    好的,我知道该怎么做...我会分享

    首先我删除了[SDWebImageManager sharedManager] 的所有证据,因为我想手动完成所有操作。

    然后[SDWebImageDownloader sharedDownloader] 请求新的,queryDiskCacheForKey (SDImageCache) 获取本地图像(磁盘或内存)

    如何:

    使用特定命名空间创建新的 imageCache

    self.imageCache = [[SDImageCache alloc] initWithNamespace:@"nameSpaceImageCacheXPTO"]; [_imageCache setMaxCacheAge:oneHour * 3]; // 3 小时

    检查图像(键)是否已经在缓存中,如果没有则请求新的并稍后保存

        /////////
        // *    Prepar Key
        /////////
    
        NSString * key = url.absoluteString;
    
        /////////
        // *    Get Local Image
        /////////
    
        [_imageCache queryDiskCacheForKey:key done:^(UIImage *image, SDImageCacheType cacheType)               {       
            if (image) {     
                completedBlock(image,nil); // return block
            }else{
    
                /////////
                // *    Request Image
                /////////
    
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
                    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url
                                                                          options:SDWebImageProgressiveDownload
                                                                         progress:^(NSInteger receivedSize, NSInteger expectedSize) {}
                                                                        completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
    
                                                                            if (finished && image){
    
                                                                                    /////////
                                                                                    // *    Save Image
                                                                                    /////////
    
                                                                              [_imageCacheProgram storeImage:image
                                                                                              recalculateFromImage:NO
                                                                                                         imageData:data
                                                                                                            forKey:key
                                                                                                            toDisk:YES];
    
                                                                                     completedBlock(image,error); // return block
                                                                            }
                                                                        }];
    
                });
            }
        }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      相关资源
      最近更新 更多