ダウンロード非同期イメージを使用しています。Web サーバーから多くの画像を取得しますが、すべて一時的なものであり、いずれもキャッシュしません。しかし、アプリは明らかにダウンロード画像を /Library/Caches に保存します。どうすれば解決できますか?
ダウンロードした画像をキャッシュしないようにする方法はありますか? それともキャッシュから削除しますか?
これらの2つの異なる方法を試しました:
#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];
}