zoukankan      html  css  js  c++  java
  • 多图片下载

    //
    //  ViewController.m
    //  多图下载案例
    //
    //  Created by kun on 16/8/11.
    //  Copyright © 2016年 kun. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    @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];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.apps.count;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *identify = @"apps";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if ( !cell )
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];
        }
        NSDictionary *dict = self.apps[indexPath.row];
        cell.textLabel.text = dict[@"name"];
        cell.detailTextLabel.text = dict[@"download"];
        __block UIImage *image = [self.images objectForKey:dict[@"icon"]];
        if ( !image )
        {
            NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *fileName = [dict[@"icon"] lastPathComponent];
            NSString *fullPath = [caches stringByAppendingPathComponent:fileName];
            __block NSData *imageData = [NSData dataWithContentsOfFile:fullPath];
            if ( !imageData )
            {
                NSBlockOperation *cacheOperation = [self.operations objectForKey:dict[@"icon"]];
                if ( !cacheOperation )
                {
                    NSBlockOperation *download = [NSBlockOperation blockOperationWithBlock:^{
                        NSURL *url = [NSURL URLWithString:dict[@"icon"]];
                        imageData = [NSData dataWithContentsOfURL:url];
                        [imageData writeToFile:fullPath atomically:YES];
                        image = [UIImage imageWithData:imageData];
                        if ( !image )
                        {
                            [self.operations removeObjectForKey:dict[@"icon"]];
                            return;
                        }
                        [self.images setObject:image forKey:dict[@"icon"]];
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                        }];
                        [self.operations removeObjectForKey:dict[@"icon"]];
                    }];
                    [self.operations setObject:download forKey:dict[@"icon"]];
                    [self.queue addOperation:download];
                }
            }
            else
            {
                image = [UIImage imageWithData:imageData];
                [self.images setObject:image forKey:dict[@"icon"]];
            }
            
        }
        cell.imageView.image = image;
        return cell;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
        [self.images removeAllObjects];
        [self.queue cancelAllOperations];
    }
    
    - (NSArray *)apps
    {
        if ( !_apps )
        {
            NSArray *temp = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];
            NSMutableArray *arrayM = [NSMutableArray arrayWithArray:temp];
            _apps = arrayM;
        }
        return _apps;
    }
    - (NSMutableDictionary *)images
    {
        if ( !_images )
        {
            _images = [NSMutableDictionary dictionary];
        }
        return _images;
    }
    - (NSOperationQueue *)queue
    {
        if ( !_queue )
        {
            _queue = [[NSOperationQueue alloc] init];
            [_queue setMaxConcurrentOperationCount:5];
        }
        return _queue;
    }
    - (NSMutableDictionary *)operations
    {
        if ( !_operations )
        {
            _operations = [NSMutableDictionary dictionary];
        }
        return _operations;
    }
    /**
     Documents:会备份,不允许
     Library:
        Caches:缓存文件
        Preferences:偏好设置,保存帐号
     tem:临时文件夹,随时会被删除
     */
    @end
  • 相关阅读:
    hotmail 收不到邮件的问题
    getaddrinfo 报错 Invalid value for ai_flags
    Avoiding Common Networking Mistakes
    关掉标准输出
    不需要 root 权限的 ping
    select 的问题
    Behavior Tree 用 Lua 实现一个最简行为树
    对 UDP 的一些思考
    Windows UDP sockets: recvfrom() fails with error 10054
    和等于某个数的所有组合
  • 原文地址:https://www.cnblogs.com/fkunlam/p/5762790.html
Copyright © 2011-2022 走看看