ios多线程下载图片

多图下载:逻辑

 

  1.内存缓存:内置字典存储

  2.磁盘缓存:写到caches缓存磁盘

  3.多线程下载图片:属性队列,存储线程(开启一次),下载、磁盘存储、内存存储、回到主线程刷新

判断: 两个结果,if else 都会执行某个操作, if(!zhi)....操作

1.数据源

LMQApps.h

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *icon;
@property (nonatomic, strong) NSString *download;

+ (instancetype)appWithDic:(NSDictionary *)dic;
+ (instancetype)appWithName:(NSString *)name icon:(NSString *)icon downlod:(NSString *)download;

+ (NSArray *)appArray;

LMQApps.m

+ (instancetype)appWithDic:(NSDictionary *)dic{
    LMQApps *app = [[LMQApps alloc] init];
    [app setValuesForKeysWithDictionary:dic];
    
    return app;
}

+ (instancetype)appWithName:(NSString *)name icon:(NSString *)icon downlod:(NSString *)download{
    LMQApps *app = [[LMQApps alloc] init];
    app.name = name;
    app.icon = icon;
    app.download = download;
    
    return app;
}

+ (NSArray *)appArray{
    NSMutableArray *arrays = [NSMutableArray array];
    [arrays addObject:[LMQApps appWithName:@"错误信息" icon:@"https://tse1-mm.cn.bing.net/th/id/OIP-C.gSzPhMra-oxijZsA?w=157&h=212&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"娱乐至死" icon:@"https://tse1-mm.cn.bing.net/th/id/OIP-C.2kXsWeOinulgZVPBxiASkAHaJ2?w=164&h=218&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"2342万"]];
    [arrays addObject:[LMQApps appWithName:@"人生的智慧" icon:@"https://tse3-mm.cn.bing.net/th/id/OIP-C.w9qzNOp08NJpE9w0W7iZCwHaHa?w=191&h=191&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"2342万"]];
    [arrays addObject:[LMQApps appWithName:@"心流" icon:@"https://tse3-mm.cn.bing.net/th/id/OIP-C.LHCFI6wFaoG5CeHOVchg2gHaJl?w=129&h=180&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"2312万"]];
    [arrays addObject:[LMQApps appWithName:@"心若菩提" icon:@"https://tse3-mm.cn.bing.net/th/id/OIP-C.rLUEgqR_KdE4fZMqfNhCoQHaLi?w=138&h=215&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"12312万"]];
    [arrays addObject:[LMQApps appWithName:@"贫穷的本质" icon:@"https://tse3-mm.cn.bing.net/th/id/OIP-C.2qXo0Pi6UdIl3ljTyoOkBgHaIy?w=142&h=180&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"112万"]];
    [arrays addObject:[LMQApps appWithName:@"遥远的救世主" icon:@"https://tse2-mm.cn.bing.net/th/id/OIP-C.ONYHL0aQn7bv5wx75U4zXgHaJ3?w=185&h=247&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"231万"]];
    [arrays addObject:[LMQApps appWithName:@"亲密关系" icon:@"https://tse1-mm.cn.bing.net/th/id/OIP-C.Sg3QhRnK_4IxiYRHtFeL7AHaKZ?w=130&h=183&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"刻意练习" icon:@"https://tse1-mm.cn.bing.net/th/id/OIP-C.ZKoRMb9P5VFAy7G75bpxZQHaKj?w=137&h=195&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"易经" icon:@"https://tse1-mm.cn.bing.net/th/id/OIP-C.IL6Mj4Gd9YmEDo1kmEh9wwHaJF?w=161&h=198&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"鬼谷子" icon:@"https://tse3-mm.cn.bing.net/th/id/OIP-C.QcuAy2NWaDzao1eZhTJFLAHaHa?w=203&h=203&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"直击本质" icon:@"https://tse2-mm.cn.bing.net/th/id/OIP-C.Tej7oaDXxTw2sFtscKvfiQAAAA?w=115&h=180&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"菜根谭" icon:@"https://tse3-mm.cn.bing.net/th/id/OIP-C.vxp58-oW1gD9Kdjv-MDHUwHaKl?w=121&h=180&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"莎士比亚十四行诗" icon:@"https://tse4-mm.cn.bing.net/th/id/OIP-C.E8k443PjefGzmGLswnIYIgAAAA?w=115&h=180&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"社会性动物" icon:@"https://tse4-mm.cn.bing.net/th/id/OIP-C.0gQE4D7BajZwbItwSha5WwHaHa?w=192&h=192&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"被讨厌的勇气" icon:@"https://tse2-mm.cn.bing.net/th/id/OIP-C.O1a09AQrH86bVcbKAf8V1gHaHa?w=212&h=212&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"围城" icon:@"https://tse3-mm.cn.bing.net/th/id/OIP-C.I8oWAWpEeUkMstN1tQ_4wwAAAA?w=116&h=180&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"飘" icon:@"https://tse4-mm.cn.bing.net/th/id/OIP-C.qfGDTC7rgmHhe6-nhleLOwAAAA?w=185&h=297&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"巴黎圣母院" icon:@"https://tse1-mm.cn.bing.net/th/id/OIP-C.EL4x-_4lPGh9E7owNI4itAHaKL?w=158&h=218&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    [arrays addObject:[LMQApps appWithName:@"百年孤独" icon:@"https://tse1-mm.cn.bing.net/th/id/OIP-C.gSzPhMra-oxijZsXGKuoEgAAAA?w=157&h=212&c=7&r=0&o=5&dpr=2&pid=1.7" downlod:@"312万"]];
    
    
    return [arrays copy];
}

2.自定义cell样式

@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *subLabel;
@property (strong, nonatomic) IBOutlet UIImageView *iconImageView;

xib样式

3.主页面 

#import "ViewController.h"
#import "LMQAppsTableViewCell.h"
#import "LMQApps.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSArray *cellArray;
@property (nonatomic, strong) NSMutableDictionary *imageDic;
@property (nonatomic, strong) NSOperationQueue *queue;
@property (nonatomic, strong) NSMutableDictionary *operationDic;

@end

@implementation ViewController
static NSString *ID = @"apps";

- (NSArray *)cellArray{
    if (!_cellArray) {
        _cellArray = [LMQApps appArray];
    }
    return _cellArray;
}

- (NSMutableDictionary *)imageDic{
    if (!_imageDic) {
        _imageDic = [NSMutableDictionary dictionary];
    }
    return _imageDic;
}

- (NSOperationQueue *)queue{
    if (!_queue) {
        _queue = [[NSOperationQueue alloc] init];
        _queue.maxConcurrentOperationCount = 5;
    }
    return _queue;
}

- (NSMutableDictionary *)operationDic{
    if (!_operationDic) {
        _operationDic = [NSMutableDictionary dictionary];
    }
    return _operationDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"LMQAppsTableViewCell" bundle:nil] forCellReuseIdentifier:ID];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LMQAppsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    LMQApps *app = self.cellArray[indexPath.row];
    cell.titleLabel.text = app.name;
    cell.subLabel.text = app.download;
    //内存
    UIImage *image = self.imageDic[app.icon];
    
    if (!image) {
        NSString *cacehs = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSString *fullPath = [cacehs stringByAppendingPathComponent:app.icon];
        //磁盘
        NSData *imageData = [NSData dataWithContentsOfFile:fullPath];
        
        if (imageData) {
            image = [UIImage imageWithData:imageData];
            [self.imageDic setValue:image forKey:app.icon];
        }else{
            image = [UIImage imageNamed:@"002"];
            NSBlockOperation *download = self.operationDic[app.icon];
            if (!download) {
                //多线程下载
                NSBlockOperation *opt = [NSBlockOperation blockOperationWithBlock:^{
                    NSURL *url = [NSURL URLWithString:app.icon];
                    NSData *blockData = [NSData dataWithContentsOfURL:url];
                    UIImage *image = [UIImage imageWithData:blockData];
                    
                    if (!image) {
                        [self.operationDic removeObjectForKey:app.icon];
                        return;
                    }
                    
                    [blockData writeToFile:fullPath atomically:YES];
                    [self.imageDic setValue:image forKey:app.icon];
                    
                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                        cell.iconImageView.image = image;
                        [self.operationDic removeObjectForKey:app.icon];
                    }];
                    
                }];
                [self.operationDic setValue:opt forKey:app.icon];
                [self.queue addOperation:opt];
            }
        }
    }
    
    cell.iconImageView.image = image;
    
    return cell;
}

//点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {//清理内存缓存
        [self.imageDic removeAllObjects];
    }else if (indexPath.row == 1){//清理磁盘缓存
        NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSFileManager *manger = [NSFileManager defaultManager];
        NSArray *subPath = [manger subpathsAtPath:caches];
        for (NSString *sub in subPath) {
            NSString *fullPath = [caches stringByAppendingPathComponent:sub];
            BOOL isDirectory;
            if ([manger fileExistsAtPath:fullPath isDirectory:&isDirectory]) {
                if (!isDirectory) {
                    [manger removeItemAtPath:fullPath error:nil];
                }
            }
        }
    }
}

//内存警告
- (void)didReceiveMemoryWarning{
    [self.imageDic removeAllObjects];
    [self.queue cancelAllOperations];
    [self.operationDic removeAllObjects];
}

@end

4.展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值