欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2016年03月28日 14:19:25   作者:blue-fly  
這篇文章主要介紹了IOS多線(xiàn)程實(shí)現(xiàn)多圖片下載(二)的相關(guān)資料,需要的朋友可以參考下

上篇文章給大家介紹了IOS多線(xiàn)程實(shí)現(xiàn)多圖片下載1,本文繼續(xù)給大家介紹ios多線(xiàn)程下載圖片。

這次是用多線(xiàn)程進(jìn)行圖片的下載與存儲(chǔ),而且考慮到下載失敗,占位圖片的問(wèn)題(第一張就是下載失敗的圖片)

閑話(huà)少說(shuō),上代碼吧,因?yàn)橛幸徊糠趾蜕洗蔚囊粯?,所以這里只上傳不一樣的

先給大家展示下效果圖:

依舊都是在ViewController.m中

1.

@interface ViewController ()
//所有數(shù)據(jù)
@property (nonatomic,strong)NSArray *apps;
//內(nèi)存緩存圖片
@property (nonatomic,strong)NSMutableDictionary *imgCache;
/**所有操作*/
@property (nonatomic,strong)NSMutableDictionary *operations;
/**隊(duì)列對(duì)象*/
@property (nonatomic,strong) NSOperationQueue *queue;
@end 

前兩個(gè)和前面的一致

operations使用來(lái)存儲(chǔ)下載圖片的線(xiàn)程操作的字典,主要作用是防止重復(fù)下載

queue則是使用多線(xiàn)程時(shí)用到的隊(duì)列

2.

- (NSOperationQueue *)queue {
if (!_queue) {
_queue = [[NSOperationQueue alloc] init];
//最大并發(fā)數(shù)
_queue.maxConcurrentOperationCount = 3;
}
return _queue;
} 

對(duì)queue的初始化,以及控制子線(xiàn)程最多為3條

3.

- (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)存中沒(méi)有圖片
//將圖片文件數(shù)據(jù)寫(xiě)入到沙盒中
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) {
//直接加載沙盒中圖片
UIImage *image = [UIImage imageWithData:data];
cell.imageView.image = image;
//存到字典(內(nèi)存)中
self.imgCache[app.icon] = image;
}else {
//下載圖片
//占位圖片
cell.imageView.image = [UIImage imageNamed:@"place.jpg"];
//先判斷是否有下載任務(wù)
//加載失敗后可以重復(fù)下載
NSOperation *operation = self.operations[app.icon];
if (operation == nil) {
//這張圖片沒(méi)有下載任務(wù)
operation = [NSBlockOperation blockOperationWithBlock:^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]];
//數(shù)據(jù)加載失敗
if(data == nil) {
//移除操作
[self.operations removeObjectForKey:app.icon];
return ;
}
UIImage *image = [UIImage imageWithData:data];
//存到內(nèi)存中
self.imgCache[app.icon] = image;
//回到主線(xiàn)程顯示圖片
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//會(huì)出現(xiàn)重復(fù)占位的問(wèn)題
//cell.imageView.image = image;
//只需找到圖片所在的行即可
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
//將圖片數(shù)據(jù)寫(xiě)入到沙盒中
[data writeToFile:file atomically:YES];
//移除操作
[self.operations removeObjectForKey:app.icon];
}];
//添加到下載隊(duì)列
[self.queue addOperation:operation];
//添加到字典
self.operations[app.icon] = operation;
}
}
}
return cell;
}

這次綁定數(shù)據(jù)的方法內(nèi)容有點(diǎn)多,因?yàn)榭紤]到了不少細(xì)節(jié),不過(guò)邏輯和上次的差不多。

有關(guān)本文給大家介紹的IOS多線(xiàn)程實(shí)現(xiàn)多圖片下載2 ,小編就給大家介紹到這里,希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論