1
我正在使用下載的異步圖像。我從我的網絡服務器上獲得了很多圖像,但都是臨時的,我不會緩存它們中的任何一個。但該應用程序顯然將下載圖像保存在/ Library/Caches中。我該如何解決這個問題?如何清除IOS應用程序庫緩存(下載的圖像)
有什麼辦法可以防止緩存下載的圖片?或從緩存中刪除它們?
我嘗試這兩種不同的方法:
#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];
}
爲什麼你需要禁用圖片緩存,如果你可以只將writeToFile:上的數據,並把它放在你通緝?緩存機制加速了未來的交易。 – CodaFi 2013-02-16 07:42:08
[[TTURLCache sharedCache] removeAll:YES]; – 2013-02-16 07:54:42
我想禁用,因爲我創建了一個大小爲600KB的簡單應用程序。加載臨時圖像後,App Store將達到15MB!並永不減少。這對於這個簡單的應用程序來說太多了。 – rcmcastro 2013-02-17 03:50:35