最近,剛忙完一個新的APP開發(fā),該APP存在大量的圖片加載,與GIF圖片。在開發(fā)過程中,少有人把精力放在圖片加載中,畢竟現(xiàn)在使用第三方SDWebImage框架的人是非常多的。但是一個精益求精的開發(fā)者,需要在不止是源碼方面的把關(guān),更需要從原材料上去選取。
通常,我們使用GIF圖片展示都是通過使用?UIImageView + SDWebImage,簡單省事,一步到位。但是,往往就是這種省心,省事的操作。使得APP的內(nèi)存在不知不覺中慢慢的增長,就好比慢性毒藥,一點一點的侵蝕,當(dāng)我們反映過來的時候,為時已晚,需要修改的地方,非常多,非常繁瑣。有點有心無力的感覺。
在此,我們需在一開始選材的時候就需要考慮清楚。原生UIImageView搭配SDWebImage在加載GIF時,會出現(xiàn)內(nèi)存暴漲的情況。我們可以通過使用 YYKit 框架中的YYAnimatedImageView來處理加載GIF內(nèi)存暴漲的問題。
在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 '協(xié)議,然后它可以用來播放多幀動畫。動畫也可以用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.(翻譯:這個視圖及時請求幀數(shù)據(jù)。當(dāng)設(shè)備有足夠的空閑內(nèi)存時,這個視圖可能會緩存一些或所有未來幀在一個內(nèi)部緩沖區(qū),以降低CPU成本。緩沖區(qū)大小會根據(jù)設(shè)備內(nèi)存的當(dāng)前狀態(tài)動態(tài)調(diào)整。)
YYAnimatedImageView介紹中,我們可以得知,該類是繼承與UIImageView的。并且擁有根據(jù)設(shè)備內(nèi)存動態(tài)調(diào)整緩沖區(qū)大小的功能,用來處理GIF加載節(jié)省非常多內(nèi)存,使用方法如下。
YYAnimatedImageView*imageview=[[YYAnimatedImageView alloc]initWithFrame:self.view.bounds];
imageview.imageURL = Box_URLWithStr(imageURL);
[self.view addSubview:imageview];
結(jié)束語:YYKit的作者貌似對該框架已經(jīng)沒有進(jìn)行維護(hù)了,在GitHub上也看到一些相關(guān)信息,如此好用的組件,還是希望作者在YYKit上進(jìn)行維護(hù)。