Coin163

首页 > ios 多线程加载图片,并实现缓存

ios 多线程加载图片,并实现缓存

相关标签: 多线程 缓存 ios

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

相关推荐:ios 多线程之GCD

一、简介 在iOS所有实现多线程的方案中,GCD应该是最有魅力的,因为GCD本身是苹果公司为多核的并行运算提出的解决方案。GCD在工作时会自动利用更多的处理器核心,以充分利用更强大的机器。GCD是Grand Central Dispatch的简称,它是基于C语言的。如果使用GCD,

转载自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.j

相关推荐:iOS开发 中的 多线程

//程序:可执行文件 //进程:一个正在运行的可执行文件,每个进程都有独立的虚拟内存空间和系统资源,包括端口权限等,并且至少包括一个主线程和任意数量的辅助线程,另外,当一个进程的主线程退出时,这个进程就结束了 //线程:一个独立的代码执行路径(线程是代码执

pg?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

原文

转载自http://blog.csdn.net/deep_explore/article/details/8144613 通过使用NSOperationQueue实现多线程加载图片,并实现缓存 新建类cc #import <Foundation/Foundation.h>@interface CC : NS

------分隔线----------------------------