多线程05-cell照片下载

  • 列表内容
//
//  HMAppsViewController.m
//  01-cell图片下载(了解)
//
//  Created by apple on 14-9-18.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#define HMAppImageFile(url) [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[url lastPathComponent]]

#import "HMAppsViewController.h"
#import "HMApp.h"

@interface HMAppsViewController ()
/**
 *  所有的应用数据
 */
@property (nonatomic, strong) NSMutableArray *apps;

/**
 *  存放所有下载操作的队列
 */
@property (nonatomic, strong) NSOperationQueue *queue;

/**
 *  存放所有的下载操作(url是key,operation对象是value)
 */
@property (nonatomic, strong) NSMutableDictionary *operations;

/**
 *  存放所有下载完的图片
 */
@property (nonatomic, strong) NSMutableDictionary *images;
@end

@implementation HMAppsViewController

#pragma mark - 懒加载
- (NSMutableArray *)apps
{
    if (!_apps) {
        // 1.加载plist
        NSString *file = [[NSBundle mainBundle] pathForResource:@"apps" ofType:@"plist"];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];

        // 2.字典 --> 模型
        NSMutableArray *appArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            HMApp *app = [HMApp appWithDict:dict];
            [appArray addObject:app];
        }

        // 3.赋值
        self.apps = appArray;
//        _apps = appArray;
    }
    return _apps;
}

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

- (NSMutableDictionary *)operations
{
    if (!_operations) {
        self.operations = [[NSMutableDictionary alloc] init];
    }
    return _operations;
}

- (NSMutableDictionary *)images
{
    if (!_images) {
        self.images = [[NSMutableDictionary alloc] init];
    }
    return _images;
}

#pragma mark - 初始化方法
- (void)viewDidLoad
{
    [super viewDidLoad];

    // 这里仅仅是block对self进行了引用,self对block没有任何引用
    [UIView animateWithDuration:2.0 animations:^{
        self.view.frame = CGRectMake(0, 0, 100, 100);
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

    // 移除所有的下载操作缓存
    [self.queue cancelAllOperations];
    [self.operations removeAllObjects];
    // 移除所有的图片缓存
    [self.images removeAllObjects];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.apps.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"app";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }

    // 取出模型
    HMApp *app = self.apps[indexPath.row];

    // 设置基本信息
    cell.textLabel.text = app.name;
    cell.detailTextLabel.text = app.download;

    // 先从images缓存中取出图片url对应的UIImage
    UIImage *image = self.images[app.icon];
    if (image) { // 说明图片已经下载成功过(成功缓存)
        cell.imageView.image = image;
    } else { // 说明图片并未下载成功过(并未缓存过)
        // 获得caches的路径, 拼接文件路径
        NSString *file = HMAppImageFile(app.icon);

        // 先从沙盒中取出图片
        NSData *data = [NSData dataWithContentsOfFile:file];
        if (data) { // 沙盒中存在这个文件
            cell.imageView.image = [UIImage imageWithData:data];
        } else { // 沙盒中不存在这个文件
            // 显示占位图片
            cell.imageView.image = [UIImage imageNamed:@"placeholder"];

            // 下载图片
            [self download:app.icon indexPath:indexPath];
        }
    }

    return cell;
}

/**
 *  下载图片
 *
 *  @param imageUrl 图片的url
 */
- (void)download:(NSString *)imageUrl indexPath:(NSIndexPath *)indexPath
{
    // 取出当前图片url对应的下载操作(operation对象)
    NSBlockOperation *operation = self.operations[imageUrl];
    if (operation) return;

    // 创建操作,下载图片
    __weak typeof(self) appsVc = self;
    operation = [NSBlockOperation blockOperationWithBlock:^{
        NSURL *url = [NSURL URLWithString:imageUrl];
        NSData *data = [NSData dataWithContentsOfURL:url]; // 下载
        UIImage *image = [UIImage imageWithData:data]; // NSData -> UIImage

        // 回到主线程
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            // 存放图片到字典中
            if (image) {
                appsVc.images[imageUrl] = image;

#warning 将图片存入沙盒中
                // UIImage --> NSData --> File(文件)
                NSData *data = UIImagePNGRepresentation(image);

                // 获得caches的路径, 拼接文件路径
//                NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[imageUrl lastPathComponent]];

                [data writeToFile:HMAppImageFile(imageUrl) atomically:YES];
//                UIImageJPEGRepresentation(<#UIImage *image#>, 1.0)
            }

            // 从字典中移除下载操作 (防止operations越来越大,保证下载失败后,能重新下载)
            [appsVc.operations removeObjectForKey:imageUrl];

            // 刷新表格
            [appsVc.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];
    }];

    // 添加操作到队列中
    [self.queue addOperation:operation];

    // 添加到字典中 (这句代码为了解决重复下载)
    self.operations[imageUrl] = operation;
}

/**
 *  当用户开始拖拽表格时调用
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 暂停下载
    [self.queue setSuspended:YES];
}

/**
 *  当用户停止拖拽表格时调用
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    // 恢复下载
    [self.queue setSuspended:NO];
}

@end
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值