当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS开发中加载大量网络图片优化方法

IOS开发中加载大量网络图片优化方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

ios开发中加载大量网络图片如何优化

1、概述

在ios下通过url读一张网络图片并不像其他编程语言那样可以直接把图片路径放到图片路径的位置就ok,而是需要我们通过一段类似流的方式去加载网络图片,接着才能把图片放入图片路径显示。比如:

-(uiimage *) getimagefromurl:(nsstring *)fileurl {
 //nslog(@"执行图片下载函数");  
 uiimage * result;  
 nsdata * data = [nsdata datawithcontentsofurl:[nsurl urlwithstring:fileurl]];
 result = [uiimage imagewithdata:data];  
 return result;
}

加载网络图片可以说是网络应用中必备的。如果单纯的去下载图片,而不去做多线程、缓存等技术去优化,加载图片时的效果与用户体验就会很差。

优化思路为:

(1)本地缓存

(2)异步加载

(3)加载完毕前使用占位图片

2、优化方法

方法1:用nsoperation开异步线程下载图片,当下载完成时替换占位图片

#import "xnviewcontroller.h"
#import "xnapp.h"

@interface xnviewcontroller ()
@property (nonatomic, strong) nsarray *applist;
@property (nonatomic, strong) nsoperationqueue *queue;
@end

@implementation xnviewcontroller
#pragma mark - 懒加载

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

//可抽取出来写到模型中
- (nsarray *)applist {
 if (!_applist) {
 //1.加载plist到数组中
 nsurl *url = [[nsbundle mainbundle] urlforresource:@"apps.plist" withextension:nil];
 nsarray *array = [nsarray arraywithcontentsofurl:url];
 //2.遍历数组
 nsmutablearray *arraym = [nsmutablearray array];
 [array enumerateobjectsusingblock: ^(id obj, nsuinteger idx, bool *stop) {
   [arraym addobject:[xnapp appwithdict:obj]]; //数组中存放的是字典, 转换为app对象后再添加到数组
 }];
 _applist = [arraym copy];
 }
 return _applist;
}

- (void)viewdidload {
 [super viewdidload];

 self.tableview.rowheight = 88;

//  nslog(@"applist-%@",_applist);
}

#pragma mark - 数据源方法
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {
 return self.applist.count;
}

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {
 static nsstring *id = @"cell";
 uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:id];

 //用模型来填充每个cell
 xnapp *app = self.applist[indexpath.row];
 cell.textlabel.text = app.name; //设置文字

 //设置图像: 模型中图像为nil时用默认图像,并下载图像. 否则用模型中的内存缓存图像.
 if (!app.image) {
 cell.imageview.image = [uiimage imagenamed:@"user_default"];

 [self downloadimg:indexpath];
 }
 else {
 //直接用模型中的内存缓存
 cell.imageview.image = app.image;
 }
// nslog(@"cell--%p", cell);

 return cell;
}

/**始终记住, 通过模型来修改显示. 而不要试图直接修改显示*/
- (void)downloadimg:(nsindexpath *)indexpath {
 xnapp *app = self.applist[indexpath.row]; //取得改行对应的模型

 [self.queue addoperationwithblock: ^{
   nsdata *imgdata = [nsdata datawithcontentsofurl:[nsurl urlwithstring:app.icon]]; //得到图像数据
   uiimage *image = [uiimage imagewithdata:imgdata];

   //在主线程中更新ui
   [[nsoperationqueue mainqueue] addoperationwithblock: ^{
     //通过修改模型, 来修改数据
     app.image = image;
     //刷新指定表格行
     [self.tableview reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationnone];
 }];
 }];
}

@end

上述代码只是做了内存缓存,还没有做本地缓存,因为这里这种方法不是重点,也不是首选方法。上面代码每次重新进入应用时,还会从网上重新下载。如果要继续优化上面的代码,需要自己去实现本地缓存。

方法2:使用第三方框架sdwebimage

特点:

依赖的库很少,功能全面。

自动实现磁盘缓存:缓存图片名字是以md5进行加密的后的名字进行命名.(因为加密那堆字串是唯一的)

加载网络图片时直接设置占位图片:[imageview sd_setimagewithurl:imageurl  placeholderimage:[uiimage imagenamed:@”xxxxx”]]。

就一个方法就实现了多线程\带缓冲等效果.(可用带参数的方法,具体可看头文件)

用sdwebimage修改上面的方法后的代码可简化为:

#pragma mark - 数据源方法
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {
 return self.applist.count;
}

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {
 static nsstring *id = @"cell";
 uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:id];

 //用模型来填充每个cell
 xnapp *app = self.applist[indexpath.row];
 cell.textlabel.text = app.name; //设置文字

// //设置图像: 模型中图像为nil时用默认图像,并下载图像. 否则用模型中的内存缓存图像.
// if (!cell.imageview.image) {
// cell.imageview.image = [uiimage imagenamed:@"user_default"];
//
// [self downloadimg:indexpath];
// }
// else {
// //直接用模型中的内存缓存
// cell.imageview.image = app.image;
// }


 //使用sdwebimage来完成上面的功能. 针对imageview.
 //一句话, 自动实现了异步下载. 图片本地缓存. 网络下载. 自动设置占位符.
 [cell.imageview sd_setimagewithurl:[nsurl urlwithstring:app.icon] placeholderimage:[uiimage imagenamed:@"user_default"]];


 return cell;
}

/**始终记住, 通过模型来修改显示. 而不要试图直接修改显示*/
//- (void)downloadimg:(nsindexpath *)indexpath {
// xnapp *app = self.applist[indexpath.row]; //取得改行对应的模型
//
// [self.queue addoperationwithblock: ^{
//   nsdata *imgdata = [nsdata datawithcontentsofurl:[nsurl urlwithstring:app.icon]]; //得到图像数据
//   uiimage *image = [uiimage imagewithdata:imgdata];
//
//   //在主线程中更新ui
//   [[nsoperationqueue mainqueue] addoperationwithblock: ^{
//     //通过修改模型, 来修改数据
//     app.image = image;
//     //刷新指定表格行
//     [self.tableview reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationnone];
// }];
// }];
//}

@end

【备注】sdwebimage中的一些参数:

*sdwebimageretryfailed = 1<< 0,  默认选项,失败后重试

*sdwebimagelowpriority = 1<< 1,  使用低优先级

*sdwebimagecachememoryonly = 1<< 2,  仅仅使用内存缓存

*sdwebimageprogressivedownload = 1<< 3,  显示现在进度

*sdwebimagerefreshcached = 1<< 4,  刷新缓存

*sdwebimagecontinueinbackground =1 << 5,  后台继续下载图像

*sdwebimagehandlecookies = 1<< 6,  处理cookie

*sdwebimageallowinvalidsslcertificates= 1 << 7,  允许无效的ssl验证

*sdwebimagehighpriority = 1<< 8,   高优先级

*sdwebimagedelayplaceholder = 1<< 9   延迟显示占位图片



感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网