【移动应用开发技术】怎么在iOS中利用多线程实现多图下载功能_第1页
【移动应用开发技术】怎么在iOS中利用多线程实现多图下载功能_第2页
【移动应用开发技术】怎么在iOS中利用多线程实现多图下载功能_第3页
【移动应用开发技术】怎么在iOS中利用多线程实现多图下载功能_第4页
【移动应用开发技术】怎么在iOS中利用多线程实现多图下载功能_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】怎么在iOS中利用多线程实现多图下载功能

这篇文章将为大家详细讲解有关怎么在iOS中利用多线程实现多图下载功能,文章内容质量较高,因此在下分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。一.模型文件代码如下//

XMGAPP.h

#import

<Foundation/Foundation.h>

@interface

XMGAPP

:

NSObject

/**

APP的名称

*/

@property

(nonatomic,

strong)

NSString

*name;

/**

APP的图片的url地址

*/

@property

(nonatomic,

strong)

NSString

*icon;

/**

APP的下载量

*/

@property

(nonatomic,

strong)

NSString

*download;

+(instancetype)appWithDict:(NSDictionary

*)dict;

@end//

XMGAPP.m

#import

"XMGAPP.h"

@implementation

XMGAPP

+(instancetype)appWithDict:(NSDictionary

*)dict

{

XMGAPP

*appM

=

[[XMGAPP

alloc]init];

//KVC

[appM

setValuesForKeysWithDictionary:dict];

return

appM;

}

@end控制器.m代码如下://

ViewController.m

#import

"ViewController.h"

#import

"XMGAPP.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

#pragma

mark

#pragma

mark

lazy

loading

-(NSOperationQueue

*)queue

{

if

(_queue

==

nil)

{

_queue

=

[[NSOperationQueue

alloc]init];

//设置最大并发数

_queue.maxConcurrentOperationCount

=

5;

}

return

_queue;

}

-(NSMutableDictionary

*)images

{

if

(_images

==

nil)

{

_images

=

[NSMutableDictionary

dictionary];

}

return

_images;

}

-(NSArray

*)apps

{

if

(_apps

==

nil)

{

//字典数组

NSArray

*arrayM

=

[NSArray

arrayWithContentsOfFile:[[NSBundle

mainBundle]pathForResource:@"apps.plist"

ofType:nil]];

//字典数组>模型数组

NSMutableArray

*arrM

=

[NSMutableArray

array];

for

(NSDictionary

*dict

in

arrayM)

{

[arrM

addObject:[XMGAPP

appWithDict:dict]];

}

_apps

=

arrM;

}

return

_apps;

}

-(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

{

static

NSString

*ID

=

@"app";

//1.创建cell

UITableViewCell

*cell

=

[tableView

dequeueReusableCellWithIdentifier:ID];

//2.设置cell的数据

//2.1

拿到该行cell对应的数据

XMGAPP

*appM

=

self.apps[indexPath.row];

//2.2

设置标题

cell.textLabel.text

=

appM.name;

//2.3

设置子标题

cell.detailTextLabel.text

=

appM.download;

//2.4

设置图标

//先去查看内存缓存中该图片时候已经存在,如果存在那么久直接拿来用,否则去检查磁盘缓存

//如果有磁盘缓存,那么保存一份到内存,设置图片,否则就直接下载

//1)没有下载过

//2)重新打开程序

UIImage

*image

=

[self.images

objectForKey:appM.icon];

if

(image)

{

cell.imageView.image

=

image;

NSLog(@"%zd处的图片使用了内存缓存中的图片",indexPath.row)

;

}else

{

//保存图片到沙盒缓存

NSString

*caches

=

[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,

NSUserDomainMask,

YES)

lastObject];

//获得图片的名称,不能包含/

NSString

*fileName

=

[appM.icon

lastPathComponent];

//拼接图片的全路径

NSString

*fullPath

=

[caches

stringByAppendingPathComponent:fileName];

//检查磁盘缓存

NSData

*imageData

=

[NSData

dataWithContentsOfFile:fullPath];

//废除

imageData

=

nil;

if

(imageData)

{

UIImage

*image

=

[UIImage

imageWithData:imageData];

cell.imageView.image

=

image;

NSLog(@"%zd处的图片使用了磁盘缓存中的图片",indexPath.row)

;

//把图片保存到内存缓存

[self.images

setObject:image

forKey:appM.icon];

//

NSLog(@"%@",fullPath);

}else

{

//检查该图片时候正在下载,如果是那么久什么都捕捉,否则再添加下载任务

NSBlockOperation

*download

=

[self.operations

objectForKey:appM.icon];

if

(download)

{

}else

{

//先清空cell原来的图片

cell.imageView.image

=

[UIImage

imageNamed:@"Snip20160221_306"];

download

=

[NSBlockOperation

blockOperationWithBlock:^{

NSURL

*url

=

[NSURL

URLWithString:appM.icon];

NSData

*imageData

=

[NSData

dataWithContentsOfURL:url];

UIImage

*image

=

[UIImage

imageWithData:imageData];

NSLog(@"%zd--下载",indexPath.row);

//容错处理

if

(image

==

nil)

{

[self.operations

removeObjectForKey:appM.icon];

return

;

}

//演示网速慢的情况

//[NSThread

sleepForTimeInterval:3.0];

//把图片保存到内存缓存

[self.images

setObject:image

forKey:appM.icon];

//NSLog(@"Download%@",[NSThread

currentThread]);

//线程间通信

[[NSOperationQueue

mainQueue]

addOperationWithBlock:^{

//cell.imageView.image

=

image;

//刷新一行

[self.tableView

reloadRowsAtIndexPaths:@[indexPath]

withRowAnimation:UITableViewRowAnimationLeft];

//NSLog(@"UI%@",[NSThread

currentThread]);

}];

//写数据到沙盒

[imageData

writeToFile:fullPath

atomically:YES];

//移除图片的下载操作

[self.operations

removeObjectForKey:appM.icon];

}];

//添加操作到操作缓存中

[self.operations

setObject:download

forKey:appM.icon];

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论