Skip to content

Instantly share code, notes, and snippets.

@fhefh2015
Last active May 17, 2016 06:12
Show Gist options
  • Save fhefh2015/dc5b8744dbcdc65f37b7f48f97072a73 to your computer and use it in GitHub Desktop.
Save fhefh2015/dc5b8744dbcdc65f37b7f48f97072a73 to your computer and use it in GitHub Desktop.
多图下载
//
// TableViewController.m
// 多图下载
//
#import "TableViewController.h"
#import "KMCGeigerCounter.h"
#import "CellData.h"
@interface TableViewController ()
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) NSMutableDictionary *cacheDatas;
@property (nonatomic, copy) NSString *savePath;
@property (nonatomic, strong) NSMutableDictionary *operationDict;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSArray *)datas {
if (_datas == nil) {
NSMutableArray *temp = [NSMutableArray array];
NSString *path = [[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil];
NSArray *arr = [NSArray arrayWithContentsOfFile:path];
for (NSDictionary *dict in arr) {
CellData *data = [CellData dataWithDict:dict];
[temp addObject:data];
}
_datas = temp;
}
return _datas;
}
- (NSMutableDictionary *)cacheDatas {
if (_cacheDatas == nil) {
_cacheDatas = [NSMutableDictionary dictionary];
}
return _cacheDatas;
}
- (NSString *)savePath {
if (_savePath == nil) {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
_savePath = path;
}
return _savePath;
}
- (NSMutableDictionary *)operationDict {
if (_operationDict == nil) {
_operationDict = [NSMutableDictionary dictionary];
}
return _operationDict;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.datas.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
CellData *data = self.datas[indexPath.row];
cell.textLabel.text = data.name;
cell.detailTextLabel.text = data.download;
NSString *fileName = [data.icon lastPathComponent];
UIImage *image = self.cacheDatas[fileName];
if (image) {
NSLog(@"m has images");
cell.imageView.image = image;
} else {
image = [UIImage imageWithContentsOfFile:[self.savePath stringByAppendingPathComponent:fileName]];
if (image) {
NSLog(@"m1 has images");
cell.imageView.image = image;
self.cacheDatas[fileName] = image;
} else {
NSLog(@"m2 has images");
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 2;
cell.imageView.image = [UIImage imageNamed:@"place"];
NSBlockOperation *op = self.operationDict[fileName];
if (op == nil) {
NSLog(@"m3 has images");
op = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"download image %@", [NSThread currentThread]);
NSURL *url = [NSURL URLWithString:data.icon];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if (urlData == nil) {
return;
}
UIImage *image = [[UIImage alloc] initWithData:urlData];
if (image) {
self.cacheDatas[fileName] = image;
}
[urlData writeToFile:[self.savePath stringByAppendingPathComponent:fileName] atomically:YES];
NSOperationQueue *queueMain = [NSOperationQueue mainQueue];
[queueMain addOperationWithBlock:^{
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
[self.operationDict removeObjectForKey:fileName];
}];
[queue addOperation:op];
self.operationDict[fileName] = op;
}
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment