ios 多线程加载图片,并实现缓存
来源:互联网 发布:阿里巴巴分销传淘宝 编辑:程序博客网 时间:2024/05/29 03:11
转载自http://blog.csdn.net/deep_explore/article/details/8144613
通过使用NSOperationQueue实现多线程加载图片,并实现缓存
新建类cc
#import <Foundation/Foundation.h>@interface CC : NSOperation{ NSString *url; NSString *imageName; UIImage *image; UIImageView *delegate;}@property (nonatomic,retain) NSString *url;@property (nonatomic,retain)NSString *imageName;@property (nonatomic,retain)UIImage *image;@property (nonatomic,retain)UIImageView *delegate;-(void)main;-(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate;@end@implementation CC@synthesize url,delegate,image,imageName;-(id) initWith:(NSString *)url imageName:(NSString *)imageName delegate:(UIImageView *)delegate{ if (self = [super init]) { self.url = [url retain]; self.imageName = imageName; self.delegate = delegate; } return self;}-(void) main{ // NSString *cachefile = [NSTemporaryDirectory() stringByAppendingPathComponent: self.imageName]; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.url]]; [data writeToFile:cachefile atomically:YES]; // self.image = [UIImage imageWithData:data]; [self performSelectorOnMainThread:@selector(ok) withObject:nil waitUntilDone:NO];}-(void)ok{ [self.delegate setImage:self.image]}@end
然后在viewcontroller中调用
//成员对列的初始化 queue=[[NSOperationQueue alloc]init];//图片资源都在数组里面了arry=[NSArray arrayWithObjects:@"http://static2.dmcdn.net/static/video/451/838/44838154:jpeg_preview_small.jpg?20120509163826", @"http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.jpg?20120509154705", @"http://static2.dmcdn.net/static/video/629/228/44822926:jpeg_preview_small.jpg?20120509181018", @"http://static2.dmcdn.net/static/video/116/367/44763611:jpeg_preview_small.jpg?20120509101749", @"http://static2.dmcdn.net/static/video/340/086/44680043:jpeg_preview_small.jpg?20120509180118", @"http://static2.dmcdn.net/static/video/666/645/43546666:jpeg_preview_small.jpg?20120412153140", @"http://static2.dmcdn.net/static/video/771/577/44775177:jpeg_preview_small.jpg?20120509183230", @"http://static2.dmcdn.net/static/video/810/508/44805018:jpeg_preview_small.jpg?20120508125339", @"http://static2.dmcdn.net/static/video/152/008/44800251:jpeg_preview_small.jpg?20120508103336", @"http://static2.dmcdn.net/static/video/694/741/35147496:jpeg_preview_small.jpg?20120508111445", @"http://static2.dmcdn.net/static/video/988/667/44766889:jpeg_preview_small.jpg?20120508130425", @"http://static2.dmcdn.net/static/video/282/467/44764282:jpeg_preview_small.jpg?20120507130637", @"http://static2.dmcdn.net/static/video/754/657/44756457:jpeg_preview_small.jpg?20120507093012", @"http://static2.dmcdn.net/static/video/831/107/44701138:jpeg_preview_small.jpg?20120506133917", @"http://static2.dmcdn.net/static/video/411/057/44750114:jpeg_preview_small.jpg?20120507014914", @"http://static2.dmcdn.net/static/video/894/547/44745498:jpeg_preview_small.jpg?20120509183004", @"http://static2.dmcdn.net/static/video/082/947/44749280:jpeg_preview_small.jpg?20120507015022", @"http://static2.dmcdn.net/static/video/833/347/44743338:jpeg_preview_small.jpg?20120509183004", @"http://static2.dmcdn.net/static/video/683/666/44666386:jpeg_preview_small.jpg?20120505111208",nil]; 在tableview中实现图片的加载-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *identifity=[NSString stringWithFormat:@"identify%d%d",indexPath.section,indexPath.row ]; UITableViewCell *cell=(UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifity]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifity]; cell.selectionStyle=UITableViewCellSelectionStyleNone; NSString *filename=[NSString stringWithFormat:@"%d",indexPath.row]; NSLog(@"filename=====%@",filename); NSString *cachefile=[NSTemporaryDirectory() stringByAppendingPathComponent:filename]; NSLog(@"cachefile=======-----%@",cachefile); UIImage *Image=[UIImage imageWithContentsOfFile:cachefile]; if (Image) { cell.imageView.image=Image; NSLog(@"有缓存,"); }else{ NSLog(@"无缓存,"); NSLog(@"arry===%@",[arry objectAtIndex:indexPath.row]); CC *o = [[CC alloc] initWith:[[arry retain] objectAtIndex:indexPath.row] imageName:[NSString stringWithFormat:@"%d",indexPath.row] delegate:cell.imageView]; [queue addOperation:o]; cell.imageView.Image=[UIImage imageNamed:@"default.png"]; //记得arry要retain,要不然滑动时回内存崩溃[[arry retain] objectAtIndex:indexPath.row] 这样写正确}}
删除图片缓存方法
#define K_LOCATION_IMG_COUNT 200+ (void)checkLocationImgAndRemove { NSString *AppPath=[NSHomeDirectory() stringByAppendingString:@"/Documents/Images/PermanentStore/"]; NSFileManager *manager = [NSFileManager defaultManager]; if([manager fileExistsAtPath:AppPath]) { NSArray *locationImgArray = [manager contentsOfDirectoryAtPath:AppPath error:nil]; if ([locationImgArray count] > [K_LOCATION_IMG_COUNT intValue]) { [manager removeItemAtPath:AppPath error:nil]; } }}@"/Documents/Images/PermanentStore/"自定,是做缓存图片的文件夹路径!
清除webview中的缓存
http://stackoverflow.com/questions/2523435/how-to-clear-uiwebview-cache
- ios 多线程加载图片,并实现缓存
- Glide加载图片并实现图片缓存
- IOS开发之异步加载网络图片并缓存本地实现瀑布流(一)
- IOS开发之异步加载网络图片并缓存本地实现瀑布流(二)
- IOS开发之异步加载网络图片并缓存本地实现瀑布流(一)
- IOS开发之异步加载网络图片并缓存本地实现瀑布流(二)
- ios 多线程加载图片
- android异步加载图片并缓存到本地实现方法
- iPhone 上实现异步加载图片并缓存代码
- android异步加载图片并缓存到本地实现方法
- android异步加载图片并缓存到本地实现方法
- 网络加载图片并实现本地缓存与获取
- iOS开发:利用SDWebImage实现图片加载与缓存
- iOS异步加载缓存图片
- iOS 多线程(4) 加载图片
- 实现三级缓存加载图片
- Volley图片加载并加入缓存处理
- Android 加载网络图片并做缓存
- 向LINUX内核添加驱动
- poj 2074
- 在Eclipse+ADT中开发Android系统的内置应用
- cocos2d-x系列笔记(5)---小精灵,走你!
- glib库hash表GHashTable介绍 .
- ios 多线程加载图片,并实现缓存
- php通过simplexml获取xml文件
- mysql索引
- 11级_Java_曹建波 03.06 Struts2_ognl
- Java之美[从菜鸟到高手演变]之Linux篇——Linux下增加swap分区的方法
- ibatis和hibernate的区别,以及如何选择~
- 季节
- [面试] C++中的static关键字 (static 上镜率非常高!)
- 导入EXCEL到数据库