#import "ViewController.h"
#import "XMGApp.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()
/** tableView的数据源*/
@property(nonatomic ,strong) NSArray *apps;
/** 图片缓存*/
@property(nonatomic ,strong) NSMutableDictionary *images;
/** */
@property(nonatomic ,strong) NSOperationQueue *queue;
@property(nonatomic ,strong) NSMutableDictionary *operations;
@end
@implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
/*
第一个参数:要下载的图片地址
第二个参数:下载模式 kNilOptions(默认的模式)
第三个参数:progress进度
receivedSize:已经下载的图片的大小
expectedSize:图片的总大小
计算图片的下载进度:1.0 *receivedSize/expectedSize
第四个参数:completed完成
*/
// [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:@"http://j3.s2.dpfile.com/pc/16488869979eb393ddad3af7c4d0d1ad%28700x700%29/thumb.jpg"] options:kNilOptions progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//
// NSLog(@"%f",1.0 *receivedSize/expectedSize);
//
// } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
// NSLog(@"%@",imageURL);
self.imageView.image = image;
// }];
}
#pragma mark -------------------
#pragma mark lazy Loading
-(NSArray *)apps
{
if (_apps == nil) {
NSArray *appsArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];
// NSLog(@"%@",appsArray);
NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:appsArray.count];
//字典转模型 字典数组--->模型数组
for (NSDictionary *dict in appsArray) {
[arrayM addObject:[XMGApp appWithDict:dict]];
}
_apps = arrayM;
}
return _apps;
}
-(NSMutableDictionary *)images
{
if (_images == nil) {
_images = [NSMutableDictionary dictionary];
}
return _images;
}
-(NSOperationQueue *)queue
{
if (_queue == nil) {
_queue = [[NSOperationQueue alloc]init];
_queue.maxConcurrentOperationCount = 5;
}
return _queue;
}
-(NSMutableDictionary *)operations
{
if (_operations == nil) {
_operations = [NSMutableDictionary dictionary];
}
return _operations;
}
#pragma mark -------------------
#pragma mark UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.apps.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.创建cell
static NSString *ID = @"app";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//2.设置cell的数据
//2.1 拿到这个cell对应的数据
XMGApp *app = self.apps[indexPath.row];
//2.2 设置文字
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
//2.3 设置图片
// [cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"Snip20151124_183"]];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"Snip20151124_183"] options:kNilOptions progress:^(NSInteger receivedSize, NSInteger expectedSize) {
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
//拿到沙盒的路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//文件名称
NSString *fileName = [app.icon lastPathComponent];
//拼接文件的全路径
NSString *fullPath = [caches stringByAppendingPathComponent:fileName];
// NSLog(@"%@",fullPath);
//3.返回cell
return cell;
}