three20图片缓存, image cache:
Date Cache:
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];
[image setBackgroundColor:[UIColor redColor]];
// [self.view addSubview:image];
NSString *imgurl=@"http://img1.cache.netease.com/www/logo/logo-ipad-icon.png";
TTURLCache *cache = [TTURLCache sharedCache];
if (![cache hasDataForKey:imgurl expires:3000]) {
NSLog(@"缓存图片");
[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgurl]]];
[cache storeImage:[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgurl]]] forURL:imgurl];
}
//取缓存图片
if ([cache hasImageForURL:imgurl fromDisk:YES]) {
NSLog(@"取缓存图片");
[image setImage:[cache imageForURL:imgurl]];
}
[self.view addSubview:image];Date Cache:
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];
[image setBackgroundColor:[UIColor redColor]];
// [self.view addSubview:image];
NSString *imgurl=@"http://img1.cache.netease.com/www/logo/logo-ipad-icon.png";
TTURLCache *cache = [TTURLCache sharedCache];
NSLog(@"begin");
if (![cache hasDataForURL:imgurl]) {
NSLog(@"缓存图片");
[cache storeData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgurl]] forURL:imgurl];
}
//取缓存图片
if ([cache hasDataForURL:imgurl]) {
NSLog(@"取缓存图片");
[image setImage:[UIImage imageWithData:[cache dataForURL:imgurl]]];
}
[self.view addSubview:image];
NSLog(@"end");
本文介绍了使用three20框架中的TTURLCache组件进行图片缓存的方法。通过实例展示了如何从网络加载图片并将其存储到缓存中,以及如何从缓存中获取已存储的图片。文中还对比了两种不同的缓存实现方式。

被折叠的 条评论
为什么被折叠?



