ios简单的图片缓存,

//简单的ios系统自带的图片缓存策略,
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",urlString,[texxImages objectAtIndex:i]]]; // [image setImageWithURL:url placeholderImage:nil]; NSURLCache *urlCache = [NSURLCache sharedURLCache]; /* 设置缓存的大小为1M*/ [urlCache setMemoryCapacity:100*1024*1024]; NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f]; //加载缓存策略 NSCachedURLResponse *response=[urlCache cachedResponseForRequest:request]; if (response!=Nil) { NSLog(@"如果有缓存输出,从缓存中获取数据"); [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; } NSURLResponse *urlResponce=nil; NSError *error=nil; NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponce error:&error]; UIImage *images=[UIImage imageWithData:data]; image.frame = CGRectMake(weizhi, 0, images.size.width, images.size.height); image.image=images; weizhi = weizhi+images.size.width; image.backgroundColor=[UIColor clearColor]; [scroll addSubview:image];

2 SDWebImage,第三方库,使用非常方便,导入库,

简单使用 [image setimage:url placeholderImage:image]

清除缓存的方法非常简单

 //清除所有缓存
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];

 

posted on 2013-07-14 15:34  songbai  阅读(434)  评论(0编辑  收藏  举报