您好,欢迎访问一九零五行业门户网

[iOS Animation]-CALayer 图像IO一_html/css_WEB-ITnose

图像io
潜伏期值得思考 - 凯文 帕萨特
在第13章“高效绘图”中,我们研究了和core graphics绘图相关的性能问题,以及如何修复。和绘图性能相关紧密相关的是图像性能。在这一章中,我们将研究如何优化从闪存驱动器或者网络中加载和显示图片。
加载和潜伏 绘图实际消耗的时间通常并不是影响性能的因素。图片消耗很大一部分内存,而且不太可能把需要显示的图片都保留在内存中,所以需要在应用运行的时候周期性地加载和卸载图片。
图片文件加载的速度被cpu和io(输入/输出)同时影响。ios设备中的闪存已经比传统硬盘快很多了,但仍然比ram慢将近200倍左右,这就需要很小心地管理加载,来避免延迟。
只要有可能,试着在程序生命周期不易察觉的时候来加载图片,例如启动,或者在屏幕切换的过程中。按下按钮和按钮响应事件之间最大的延迟大概是200ms,这比动画每一帧切换的16ms小得多。你可以在程序首次启动的时候加载图片,但是如果20秒内无法启动程序的话,ios检测计时器就会终止你的应用(而且如果启动大于2,3秒的话用户就会抱怨了)。
有些时候,提前加载所有的东西并不明智。比如说包含上千张图片的图片传送带:用户希望能够能够平滑快速翻动图片,所以就不可能提前预加载所有图片;那样会消耗太多的时间和内存。
有时候图片也需要从远程网络连接中下载,这将会比从磁盘加载要消耗更多的时间,甚至可能由于连接问题而加载失败(在几秒钟尝试之后)。你不能够在主线程中加载网络造成等待,所以需要后台线程。
线程加载 在第12章“性能调优”我们的联系人列表例子中,图片都非常小,所以可以在主线程同步加载。但是对于大图来说,这样做就不太合适了,因为加载会消耗很长时间,造成滑动的不流畅。滑动动画会在主线程的run loop中更新,所以会有更多运行在渲染服务进程中cpu相关的性能问题。
清单14.1显示了一个通过 uicollectionview 实现的基础的图片传送器。图片在主线程中 -collectionview:cellforitematindexpath: 方法中同步加载(见图14.1)。
清单14.1 使用uicollectionview实现的图片传送器



#import viewcontroller.h@interface viewcontroller() @property (nonatomic, copy) nsarray *imagepaths;@property (nonatomic, weak) iboutlet uicollectionview *collectionview;@end@implementation viewcontroller- (void)viewdidload{ //set up data self.imagepaths = [[nsbundle mainbundle] pathsforresourcesoftype:@png indirectory:@vacation photos]; //register cell class [self.collectionview registerclass:[uicollectionviewcell class] forcellwithreuseidentifier:@cell];}- (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section{ return [self.imagepaths count];}- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath{ //dequeue cell uicollectionviewcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:@cell forindexpath:indexpath]; //add image view const nsinteger imagetag = 99; uiimageview *imageview = (uiimageview *)[cell viewwithtag:imagetag]; if (!imageview) { imageview = [[uiimageview alloc] initwithframe: cell.contentview.bounds]; imageview.tag = imagetag; [cell.contentview addsubview:imageview]; } //set image nsstring *imagepath = self.imagepaths[indexpath.row]; imageview.image = [uiimage imagewithcontentsoffile:imagepath]; return cell;}@end






图14.1 运行中的图片传送器
传送器中的图片尺寸为800x600像素的png,对iphone5来说,1/60秒要加载大概700kb左右的图片。当传送器滚动的时候,图片也在实时加载,于是(预期中的)卡动就发生了。时间分析工具(图14.2)显示了很多时间都消耗在了 uiimage 的 +imagewithcontentsoffile: 方法中了。很明显,图片加载造成了瓶颈。
其它类似信息

推荐信息