Question

I'm using SDWebImage for a while caching all my images but now i want to have more than one cache to to group various types of images. For example three kind of caches with several images each, so in runtime i want to clear one of them or have different setMaxCacheAge

Example: types images = car images is one type, motorcycle is another... airplanes other.. like this. After i store this images i want delete or clear cache only of the motorcycle images (one type)

Now I have this but is for every images cached:

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

...

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

-

I saw this but is really that i want?

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

Using Asynchronous Image Caching Independently

Was it helpful?

Solution

Ok I figure out how to do...and I will share

First i removed all evidences of [SDWebImageManager sharedManager], because I want to do everything manually.

Then [SDWebImageDownloader sharedDownloader] to request new one and queryDiskCacheForKey (SDImageCache) to get local image (disk or memory)

How:

Create new imageCache with specific namespace

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

Check if the image (key) is already in cache if not will request new one and save later

    /////////
    // *    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
                                                                        }
                                                                    }];

            });
        }
    }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top