NSURLRequest、NSURLConnection、および NSURLRequestReturnCacheDataElseLoad ポリシーを使用して画像キャッシュを実装するためのサンプル コードはありますか?
次のコードを使用していますが、キャッシュが発生していないようです。いつもURLから画像を取得しています。ここで何が問題なのか教えてください:
NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://i54.tinypic.com/10pd2jk.png"]];
NSData *data=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSLog(@"Received %d bytes", [data length]);
[req setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
data=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSLog(@"Received %d bytes", [data length]);
[[NSURLCache sharedURLCache] setMemoryCapacity:1024*1024*10];
UIImage *myImage = [UIImage imageWithData:data];
UIImageView *sd = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
sd.image = myImage;
[self.view addSubview:sd];