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