最近,刚忙完一个新的APP开发,该APP存在大量的图片加载,与GIF图片。在开发过程中,少有人把精力放在图片加载中,毕竟现在使用第三方SDWebImage框架的人是非常多的。但是一个精益求精的开发者,需要在不止是源码方面的把关,更需要从原材料上去选取。
通常,我们使用GIF图片展示都是通过使用 UIImageView + SDWebImage,简单省事,一步到位。但是,往往就是这种省心,省事的操作。使得APP的内存在不知不觉中慢慢的增长,就好比慢性毒药,一点一点的侵蚀,当我们反映过来的时候,为时已晚,需要修改的地方,非常多,非常繁琐。有点有心无力的感觉。
在此,我们需在一开始选材的时候就需要考虑清楚。原生UIImageView搭配SDWebImage在加载GIF时,会出现内存暴涨的情况。我们可以通过使用 YYKit 框架中的YYAnimatedImageView来处理加载GIF内存暴涨的问题。
在YYAnimatedImageView头文件中,有这么一段介绍:It is a fully compatible `UIImageView` subclass. If the `image` or `highlightedImage` property adopt to the `YYAnimatedImage` protocol,then it can be used to play the multi-frame animation.The animation can also be controlled with the UIImageView methods `-startAnimating`, `-stopAnimating` and `-isAnimating`.(翻译:它是一个完全兼容的UIImageView子类。如果' image '或'highlightedImage '属性采用' YYAnimatedImage '协议,然后它可以用来播放多帧动画。动画也可以用UIImageView方法' -startAnimating ', ' -stopAnimating '和' -isAnimating '来控制。)
This view request the frame data just in time. When the device has enough free memory, this view may cache some or all future frames in an inner buffer for lower CPU cost.Buffer size is dynamically adjusted based on the current state of the device memory.(翻译:这个视图及时请求帧数据。当设备有足够的空闲内存时,这个视图可能会缓存一些或所有未来帧在一个内部缓冲区,以降低CPU成本。缓冲区大小会根据设备内存的当前状态动态调整。)
YYAnimatedImageView介绍中,我们可以得知,该类是继承与UIImageView的。并且拥有根据设备内存动态调整缓冲区大小的功能,用来处理GIF加载节省非常多内存,使用方法如下。
YYAnimatedImageView*imageview=[[YYAnimatedImageView alloc]initWithFrame:self.view.bounds];
imageview.imageURL = Box_URLWithStr(imageURL);
[self.view addSubview:imageview];
结束语:YYKit的作者貌似对该框架已经没有进行维护了,在GitHub上也看到一些相关信息,如此好用的组件,还是希望作者在YYKit上进行维护。