1
votes

I am working with download async images. I get a lot images from my webserver but all are temporary and I won't cache any of them. But the app apparently save the download images in /Library/Caches. How can I resolve that?

Are there any way to prevent cache downloaded images? Or delete them from cache?

I tried these two different methods:

#pragma mark - Method 1

- (void)method1{
    NSURL *imageURL = [NSURL URLWithString:self.photoLink];
    [self setImage:[UIImage imageNamed:@"nut"] forState:UIControlStateNormal];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^{
        NSData *data = [NSData dataWithContentsOfURL:imageURL options:NSDataReadingUncached error:NULL];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self setImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
            });
        });
    //}
}

#pragma mark - Method 2

- (void)method2{

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    // The Placeholder
    [self setImage:[UIImage imageNamed:@"nut"] forState:UIControlStateNormal];

    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                            selector:@selector(loadDataAsyc)
                                                                              object:nil];
    [queue addOperation:operation];

    [operation release];
}
-(void)loadDataAsyc{

    // create a local autorelease pool since this code runs not on main thread
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.photoLink] options:NSDataReadingUncached error:NULL];
    UIImage *image = [UIImage imageWithData:data];

    [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:NO];

    [pool release];
}
-(void)showImage:(UIImage *)image{
    [self setImage:image forState:UIControlStateNormal];
}
1
Why would you want to disable the image cache, if you could just writeToFile: on the data and put it where you wanted? The caching mechanism speeds up future transactions.CodaFi
[[TTURLCache sharedCache] removeAll:YES];Ravindra Bagale
I want to disable because I create a simple app which size is 600KB. After load the temporary images the App Storage goes to 15MB! and never reduce. This is too much for this simple app.rcmstark

1 Answers

0
votes

The cache will be emptied when it needs to be, i.e. when the application receives a Low Memory Warning. There is no reason for you to do this yourself, and there is no public API available.


NOTE
When the os manages something for you, you don't need to worry about it.


But if you really want to clear your cache then follow this

NSFileManager *filemgr;

filemgr = [NSFileManager defaultManager];

if ([filemgr removeItemAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] error: NULL]  == YES)
    NSLog (@"Remove successful");
else
    NSLog (@"Remove failed");

[filemgr createDirectoryAtPath: [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] withIntermediateDirectories:NO attributes:nil error:nil];