- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIden = @"myCell";
static BOOL nibsRegistered = NO;
if (!nibsRegistered) {
UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:cellIden];
nibsRegistered = YES;
}
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIden];
}
News *news = [array objectAtIndex:indexPath.row];
cell.NewsTitle.text = news.title;
cell.NewsDetail.text = news.detail;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSString *imageLocalFile = [self getImageDocumentsPath:news.imageUrl]; //图片本地的地址
NSLog(@"-------%@",imageLocalFile);
NSFileManager *fileManger = [NSFileManager defaultManager];
//判断图片本地是否存在,若存在则优先加载本地,否则加载发送网络
if ([fileManger fileExistsAtPath:imageLocalFile]) {
cell.imageView.image = [UIImage imageWithContentsOfFile:imageLocalFile]; //加载本地
}
else
{
cell.myActivity.hidesWhenStopped = YES;
[cell.myActivity startAnimating];
[self loadImage:news.imageUrl withIndexPath:indexPath]; //网络加载图片
}
return cell;
}
- (void)loadImage:(NSString *)imageUrl withIndexPath:(NSIndexPath *)indexPath
{
//异步加载图片
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//加载图片
NSURL *url = [NSURL URLWithString:imageUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
if (data != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *imageLocalFile = [self getImageDocumentsPath:imageUrl];
if ([data writeToFile:imageLocalFile atomically:YES]==NO) { // 图片本地化保存
NSLog(@"图片本地化保存失败!");
}
CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageWithData:data];
[cell.myActivity stopAnimating];
});
}
});
}
- (NSString *)getImageDocumentsPath:(NSString *)imageUrl
{
NSLog(@"%@",imageUrl); //imageUrl形如:http://img1.cache.netease.com/3g/2014/8/11/2014081100332746e6f.jpg
NSArray *array_ = [imageUrl componentsSeparatedByString:@"/"]; //网络imageUrl通过分隔符分隔开来,
NSString *string = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[array_ lastObject]]; / /组装本地化存储一个存储网络图片的地址,形如:/Users/administrator/Library/Application Support/iPhone Simulator/7.0.3/Applications/16071117-BE38-4F37-8FEF-4AF438C053A0/Documents/2014090111135873241.jpg
return string;
}
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIden = @"myCell";
static BOOL nibsRegistered = NO;
if (!nibsRegistered) {
UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:cellIden];
nibsRegistered = YES;
}
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIden];
}
News *news = [array objectAtIndex:indexPath.row];
cell.NewsTitle.text = news.title;
cell.NewsDetail.text = news.detail;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSString *imageLocalFile = [self getImageDocumentsPath:news.imageUrl]; //图片本地的地址
NSLog(@"-------%@",imageLocalFile);
NSFileManager *fileManger = [NSFileManager defaultManager];
//判断图片本地是否存在,若存在则优先加载本地,否则加载发送网络
if ([fileManger fileExistsAtPath:imageLocalFile]) {
cell.imageView.image = [UIImage imageWithContentsOfFile:imageLocalFile]; //加载本地
}
else
{
cell.myActivity.hidesWhenStopped = YES;
[cell.myActivity startAnimating];
[self loadImage:news.imageUrl withIndexPath:indexPath]; //网络加载图片
}
return cell;
}
- (void)loadImage:(NSString *)imageUrl withIndexPath:(NSIndexPath *)indexPath
{
//异步加载图片
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//加载图片
NSURL *url = [NSURL URLWithString:imageUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
if (data != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *imageLocalFile = [self getImageDocumentsPath:imageUrl];
if ([data writeToFile:imageLocalFile atomically:YES]==NO) { // 图片本地化保存
NSLog(@"图片本地化保存失败!");
}
CustomCell *cell = (CustomCell *)[self.myTableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageWithData:data];
[cell.myActivity stopAnimating];
});
}
});
}
- (NSString *)getImageDocumentsPath:(NSString *)imageUrl
{
NSLog(@"%@",imageUrl); //imageUrl形如:http://img1.cache.netease.com/3g/2014/8/11/2014081100332746e6f.jpg
NSArray *array_ = [imageUrl componentsSeparatedByString:@"/"]; //网络imageUrl通过分隔符分隔开来,
NSString *string = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),[array_ lastObject]]; / /组装本地化存储一个存储网络图片的地址,形如:/Users/administrator/Library/Application Support/iPhone Simulator/7.0.3/Applications/16071117-BE38-4F37-8FEF-4AF438C053A0/Documents/2014090111135873241.jpg
return string;
}