iOS多線程實(shí)現(xiàn)多圖下載功能
本文實(shí)例為大家分享了iOS多線程實(shí)現(xiàn)多圖下載功能的具體代碼,供大家參考,具體內(nèi)容如下
一.模型文件代碼如下
// 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的數(shù)據(jù)源 */
@property (nonatomic, strong) NSArray *apps;
/** 內(nèi)存緩存 */
@property (nonatomic, strong) NSMutableDictionary *images;
/** 隊(duì)列 */
@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];
//設(shè)置最大并發(fā)數(shù)
_queue.maxConcurrentOperationCount = 5;
}
return _queue;
}
-(NSMutableDictionary *)images
{
if (_images == nil) {
_images = [NSMutableDictionary dictionary];
}
return _images;
}
-(NSArray *)apps
{
if (_apps == nil) {
//字典數(shù)組
NSArray *arrayM = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"apps.plist" ofType:nil]];
//字典數(shù)組---->模型數(shù)組
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.創(chuàng)建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//2.設(shè)置cell的數(shù)據(jù)
//2.1 拿到該行cell對應(yīng)的數(shù)據(jù)
XMGAPP *appM = self.apps[indexPath.row];
//2.2 設(shè)置標(biāo)題
cell.textLabel.text = appM.name;
//2.3 設(shè)置子標(biāo)題
cell.detailTextLabel.text = appM.download;
//2.4 設(shè)置圖標(biāo)
//先去查看內(nèi)存緩存中該圖片時(shí)候已經(jīng)存在,如果存在那么久直接拿來用,否則去檢查磁盤緩存
//如果有磁盤緩存,那么保存一份到內(nèi)存,設(shè)置圖片,否則就直接下載
//1)沒有下載過
//2)重新打開程序
UIImage *image = [self.images objectForKey:appM.icon];
if (image) {
cell.imageView.image = image;
NSLog(@"%zd處的圖片使用了內(nèi)存緩存中的圖片",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) ;
//把圖片保存到內(nèi)存緩存
[self.images setObject:image forKey:appM.icon];
// NSLog(@"%@",fullPath);
}else
{
//檢查該圖片時(shí)候正在下載,如果是那么久什么都捕捉,否則再添加下載任務(wù)
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);
//容錯(cuò)處理
if (image == nil) {
[self.operations removeObjectForKey:appM.icon];
return ;
}
//演示網(wǎng)速慢的情況
//[NSThread sleepForTimeInterval:3.0];
//把圖片保存到內(nèi)存緩存
[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]);
}];
//寫數(shù)據(jù)到沙盒
[imageData writeToFile:fullPath atomically:YES];
//移除圖片的下載操作
[self.operations removeObjectForKey:appM.icon];
}];
//添加操作到操作緩存中
[self.operations setObject:download forKey:appM.icon];
//添加操作到隊(duì)列中
[self.queue addOperation:download];
}
}
}
//3.返回cell
return cell;
}
-(void)didReceiveMemoryWarning
{
[self.images removeAllObjects];
//取消隊(duì)列中所有的操作
[self.queue cancelAllOperations];
}
//1.UI很不流暢 --- > 開子線程下載圖片
//2.圖片重復(fù)下載 ---> 先把之前已經(jīng)下載的圖片保存起來(字典)
//內(nèi)存緩存--->磁盤緩存
//3.圖片不會刷新--->刷新某行
//4.圖片重復(fù)下載(圖片下載需要時(shí)間,當(dāng)圖片還未完全下載之前,又要重新顯示該圖片)
//5.數(shù)據(jù)錯(cuò)亂 ---設(shè)置占位圖片
/*
Documents:會備份,不允許
Libray
Preferences:偏好設(shè)置 保存賬號
caches:緩存文件
tmp:臨時(shí)路徑(隨時(shí)會被刪除)
*/
@end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡單講解Objective-C的基本特性及其內(nèi)存管理方式
這篇文章主要介紹了簡單講解Objective-C的基本特性及其內(nèi)存管理方式,雖然Swift語言出現(xiàn)后iOS和Mac OS應(yīng)用開發(fā)方面Objective-C正在成為過去時(shí),但現(xiàn)有諸多項(xiàng)目仍然在使用,需要的朋友可以參考下2016-01-01
關(guān)于iOS 11下app圖標(biāo)變空白問題的解決方法
升級到iOS11系統(tǒng)下自己的項(xiàng)目桌面app圖標(biāo)不見了,通過查找相關(guān)的資料終于找到了解決方法,下面這篇文章主要給大家介紹了關(guān)于iOS 11下app圖標(biāo)變空白問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12
iOS列表上拉(平滑加載數(shù)據(jù))自動(dòng)加載數(shù)據(jù)的問題解決
這篇文章主要給大家介紹了關(guān)于iOS列表上拉(平滑加載數(shù)據(jù))自動(dòng)加載數(shù)據(jù)問題的相關(guān)資料,本文實(shí)現(xiàn)的效果很多app都用的這種效果,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07
Flutter開發(fā)Widgets?之?PageView使用示例
這篇文章主要為大家介紹了Flutter開發(fā)Widgets?之?PageView使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
ios基于MJRefresh實(shí)現(xiàn)上拉刷新和下拉加載動(dòng)畫效果
本篇文章主要介紹了ios基于MJRefresh實(shí)現(xiàn)上拉刷新和下拉加載動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
iOS runtime動(dòng)態(tài)添加方法示例詳解
Runtime是想要做好iOS開發(fā),或者說是真正的深刻的掌握OC這門語言所必需理解的東西。下面這篇文章主要給大家介紹了關(guān)于iOS runtime動(dòng)態(tài)添加方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2018-01-01
解析iOS應(yīng)用的UI開發(fā)中懶加載和xib的簡單使用方法
這篇文章主要介紹了解析iOS應(yīng)用的UI開發(fā)中懶加載和xib的簡單使用方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01

