国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

IOS多線程實(shí)現(xiàn)多圖片下載(一)

2019-10-21 18:55:21
字體:
供稿:網(wǎng)友
這篇文章主要介紹了IOS多線程實(shí)現(xiàn)多圖片下載(一)的相關(guān)資料,需要的朋友可以參考下
 

在沒有步入正文之前先給大家展示下效果圖,如果大家覺得很滿意請(qǐng)繼續(xù)往下閱讀全文。

IOS,多線程,圖片下載

大家可以看到這個(gè)界面很簡(jiǎn)單,其實(shí)就是UITableView的布局,但是難點(diǎn)是在于如何從網(wǎng)上下載這些圖片,下載之后應(yīng)如何進(jìn)行存儲(chǔ)!

我們一步一步進(jìn)行解析,先從單線程(主線程)進(jìn)行多圖片下載我們布局上的文字及圖片的地址從plist文件中進(jìn)行讀取

IOS,多線程,圖片下載

根據(jù)結(jié)構(gòu),我們自定義一個(gè)數(shù)據(jù)模型文件

DDZApp.h

#import <Foundation/Foundation.h>@interface DDZApp : NSObject//圖標(biāo)@property (nonatomic,strong) NSString *icon;//名字@property (nonatomic,strong) NSString *name;//下載量@property (nonatomic,strong) NSString *download;+ (instancetype)appWithDict:(NSDictionary *)dict;@end 

DDZApp.m

#import "DDZApp.h"@implementation DDZApp+ (instancetype)appWithDict:(NSDictionary *)dict {DDZApp *app = [[self alloc] init];[app setValuesForKeysWithDictionary:dict];return app;}@end 

以下的都是視圖控制器中的代碼

ViewController.m

1.

@interface ViewController ()//所有數(shù)據(jù)@property (nonatomic,strong)NSArray *apps;//內(nèi)存緩存圖片@property (nonatomic,strong)NSMutableDictionary *imgCache;@end 

第一個(gè)屬性用于存儲(chǔ)讀取plist文件中的內(nèi)容,設(shè)置為屬性保存起來,就可以不用重復(fù)讀取

第二個(gè)屬性用于保存從網(wǎng)上下載下來的圖片,也是為了不用重復(fù)讀取

2.

@implementation ViewController//讀取數(shù)據(jù)- (NSArray *)apps {if (!_apps) {//從plist文件中讀取數(shù)據(jù)NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];NSMutableArray *appArray = [NSMutableArray array];for (NSDictionary *dict in dictArray) {[appArray addObject:[DDZApp appWithDict:dict]];}_apps = appArray;}return _apps;}//緩存圖片- (NSMutableDictionary *)imgCache {if (!_imgCache) {//初始化_imgCache = [NSMutableDictionary dictionary];}return _imgCache;}

這兩個(gè)方法都是為了初始化剛才的兩個(gè)屬性

3.

#pragma mark - 數(shù)據(jù)源方法- (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];DDZApp *app = self.apps[indexPath.row];cell.textLabel.text = app.name;cell.detailTextLabel.text = app.download;//先從內(nèi)存中取出圖片UIImage *image = self.imgCache[app.icon];if (image) {cell.imageView.image = image;}else {//內(nèi)存中沒有圖片//將圖片文件數(shù)據(jù)寫入到沙盒中NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];//獲得文件名NSString *filename = [app.icon lastPathComponent];//計(jì)算出文件的全路徑NSString *file = [cachesPath stringByAppendingPathComponent:filename];//加載沙盒的文件數(shù)據(jù)NSData *data = [NSData dataWithContentsOfFile:file];//判斷沙盒中是否有圖片if (data) {//直接加載沙盒中圖片cell.imageView.image = [UIImage imageWithData:data];//存到字典(內(nèi)存)中self.imgCache[app.icon] = cell.imageView.image;}else {//下載圖片data = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]];cell.imageView.image = [UIImage imageWithData:data];//存到內(nèi)存中self.imgCache[app.icon] = cell.imageView.image;//將圖片數(shù)據(jù)寫入到沙盒中[data writeToFile:file atomically:YES];}}return cell;}

這兩個(gè)方法是UITableView必須要實(shí)現(xiàn)的方法

第一個(gè)是返回?cái)?shù)據(jù)量,沒什么好說的

第二個(gè)是綁定數(shù)據(jù)

具體的流程看下圖

IOS,多線程,圖片下載

以上內(nèi)容針對(duì)IOS多線程實(shí)現(xiàn)多圖片下載(一)的相關(guān)介紹,希望對(duì)大家有所幫助,下篇文章繼續(xù)給大家介紹IOS多線程實(shí)現(xiàn)多圖片下載(二),感興趣的朋友請(qǐng)持續(xù)關(guān)注。



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 井研县| 睢宁县| 固镇县| 江陵县| 尼玛县| 江源县| 辽宁省| 沙湾县| 阜城县| 墨竹工卡县| 宁陵县| 裕民县| 昌宁县| 会理县| 盐亭县| 澳门| 辛集市| 库尔勒市| 榕江县| 临澧县| 宁阳县| 武平县| 奉贤区| 镇安县| 武定县| 图片| 岱山县| 永年县| 淄博市| 永年县| 榆社县| 宁明县| 横山县| 延寿县| 海宁市| 连江县| 健康| 临海市| 南江县| 乐业县| 呼和浩特市|