网络图片下载的实例
有一个plist描述图片信息:
<array>
<dict>
<key>name</key>
<string>植物大战僵尸</string>
<key>icon</key>
<string>http://p16.qhimg.com/dr/48_48_/t0125e8d438ae9d2fbb.png</string>
<key>download</key>
<string>10311万</string>
</dict>
<dict>
<key>name</key>
<string>捕鱼达人2</string>
<key>icon</key>
<string>http://p19.qhimg.com/dr/48_48_/t0101e2931181bb540d.png</string>
<key>download</key>
<string>9982万</string>
</dict>
...
</array>
创建一个字典模型:
AppItem.h
#import <Foundation/Foundation.h>
@interface AppItem : NSObject
@property (strong,nonatomic) NSString *name;
@property (strong,nonatomic) NSString *icon;
@property (strong,nonatomic) NSString *download;
+ (instancetype)itemWithDict:(NSDictionary*)dict;
@end
AppItem.m
#import "AppItem.h"
@implementation AppItem
+ (instancetype)itemWithDict:(NSDictionary*)dict
{
AppItem *item = [[AppItem alloc]init];
[item setValuesForKeysWithDictionary:dict];
return item;
}
@end
ViewController.h
#import "ViewController.h"
#import "AppItem.h"
@interface ViewController ()
@property (strong,nonatomic)NSArray<AppItem*> *appItems;
@property (strong,nonatomic)NSMutableDictionary *imagesDict;
@property (strong,nonatomic)NSMutableDictionary *operationDict;
@property (strong,nonatomic)NSOperationQueue *queue;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (NSArray<AppItem *> *)appItems
{
if(_appItems == nil)
{
NSArray *dicts = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];
NSMutableArray *items = [NSMutableArray array];
for(NSDictionary *dict in dicts)
{
AppItem *appItem = [AppItem itemWithDict:dict];
[items addObject:appItem];
}
_appItems = items;
}
return _appItems;
}
- (NSMutableDictionary *)imagesDict
{
if(_imagesDict == nil)
{
_imagesDict = [NSMutableDictionary dictionary];
}
return _imagesDict;
}
- (NSMutableDictionary *)operationDict
{
if(_operationDict == nil)
{
_operationDict = [NSMutableDictionary dictionary];
}
return _operationDict;
}
- (NSOperationQueue *)queue
{
if(_queue == nil)
{
_queue = [[NSOperationQueue alloc]init];
}
return _queue;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.appItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"appCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellId];
__block UIImage *iconImage = [self.imagesDict objectForKey:self.appItems[indexPath.row].name];
if(iconImage == nil)
{
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *fileName = [self.appItems[indexPath.row].icon lastPathComponent];
NSString *fileFullPath = [cachePath stringByAppendingPathComponent:fileName];
NSData *cacheData = [NSData dataWithContentsOfFile:fileFullPath];
iconImage = [UIImage imageWithData:cacheData];
if(iconImage == nil)
{
iconImage = [UIImage imageNamed:@"defaultImage"];
__block NSBlockOperation *downloadOp = [self.operationDict objectForKey:self.appItems[indexPath.row].name];
if(downloadOp == nil)
{
cell.imageView.image = [UIImage imageNamed:@"defaultImage"];
downloadOp = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"%ld-----图片下载-----%@",indexPath.row,[NSThread currentThread]);
NSURL *url = [NSURL URLWithString:self.appItems[indexPath.row].icon];
NSData *downloadData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:downloadData];
if(image == nil)
{
NSLog(@"%ld-----图片错误,没能被下载-----%@",indexPath.row,[NSThread currentThread]);
[self.operationDict removeObjectForKey:self.appItems[indexPath.row].name];
return;
}
[NSThread sleepForTimeInterval:1.0];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
[self.imagesDict setObject:image forKey:self.appItems[indexPath.row].name];
[downloadData writeToFile:fileFullPath atomically:YES];
[self.operationDict removeObjectForKey:self.appItems[indexPath.row].name];
}];
[self.operationDict setObject:downloadOp forKey:self.appItems[indexPath.row].name];
[self.queue addOperation:downloadOp];
}
else
{
NSLog(@"%ld-----图片已经在下载,不会重复下载-----%@",indexPath.row,[NSThread currentThread]);
}
}
else
{
//cell.imageView.image = iconImage;
NSLog(@"%ld-----图片在本地找到",indexPath.row);
}
}
else
{
//cell.imageView.image = iconImage;
NSLog(@"%ld-----图片已经存入缓存",indexPath.row);
}
cell.imageView.image = iconImage;
cell.textLabel.text = self.appItems[indexPath.row].name;
cell.detailTextLabel.text = self.appItems[indexPath.row].download;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (void)didReceiveMemoryWarning
{
[self.imagesDict removeAllObjects];
[self.queue cancelAllOperations];
}
@end