IOS高性能圖片緩存庫:DotCImageManager

jopen 10年前發布 | 8K 次閱讀 iOS開發 移動開發 DotCImageManager

DotCImageManager 是一個高性能的圖片緩存庫,采用游戲開發中的資源管理策略:一份資源只存在一份內存實例,在適當的時候進行自動資源回收;提供參考尺寸匹配機制,有效減少服務器請求;使用SQLite存儲圖片,方便圖片持久化管理;提供DotCImageView,方便集成和使用。

集成

  • 從DotCIntegrateAdapter繼承,添加YourIntegrateAdapterClass類
  • 重寫下面接口:
- (void) request:(NSString*)image width:(int)w height:(int)h info:(void*)info

請求圖片數據,image為圖片ID,w和h為參考尺寸。當請求完圖片或者失敗后,需要調用onRequest:(NSData)imageData info:(void)info,其中info為請求時提供的info參數。

- (UIImage*) getPlaceHolder:(NSString*)name

獲取占位圖片,使用DotCImageView時使用。

- (NSString*) getDatabasePath

獲取數據庫路徑,該接口為可選。

- (int) getMaxMemoryCacheSize

獲取內存緩存的最大限制,該接口為可選。默認為10M。

  • 初始化
    在工程的初始化代碼地方添加:
...
   // Setup integrate adapter
   [DotCImageManager setIntegrateAdapter:[[[YourIntegrateAdapterClass alloc] init] auturelease]];
   // Initialize DotCImageManager, DotCImageManager is singleton instance
   DOTC_IMAGE_MANAGER;
   ...
  • 使用DotCImageView加載圖片,或者直接通過DOTC_IMAGE_MANAGER獲取圖片

文檔

DotC_ImageManager中主要包含 DotCImanageManager,DotCImageView,DotCIntegrateAdapter;DotCDelegatorManager,DotCDelegator,DotCDelegatorArguments;Delegator 部分也是一個可獨立使用的庫,提供DotCImageManager獲取圖片的異步回調策略。

DotCImanageManager

圖片緩存核心類,提供對外的請求接口和對緩存的操作接口。通過DOTC_IMAGE_MANAGER獲取本類的Singleton實例。

- (void) retrieveImage:(NSString*)key delegatorID:(NSString*)delegatorID;

根據key獲取原始圖片,delegatorID為回調ID,當圖片獲取成功或者失敗時Delegator被調用。

- (void) retrieveImage:(NSString*)key delegatorID:(NSString*)delegatorID width:(float)w height:(float)h;

根據key和參考尺寸w*h獲取圖片,delegatorID為回調ID,當圖片獲取成功或者失敗時Delegator被調用。

- (void) clearLocalCache;

清除內存緩存。注意:只清理內存中的緩存。

- (void) clearCache:(float)daysAgo;

清除指定時間前的圖片,daysAgo單位為天,如果為0則清除所有圖片。
注意:
1.當一個圖片被使用時,會更新這個圖片的最新時間,保證在清除緩存時總是清除的最不常用的圖片。
2.該接口只清除數據庫中的緩存。

- (int)  getCacheSize;

獲取當前數據庫中的緩存大小,以byte為單位。

- (int)  getLocalCacheSize;

獲取當前內存中的緩存大小,以byte為單位。

+ (void) setIntegrateAdapter:(DotCIntegrateAdapter*)adapter;

設置集成適配器實例。

+ (DotCIntegrateAdapter*)integrateAdapter;

獲取集成適配器實例。

DotCImageView

從UIImageView繼承,提供針對DotCImageManager的UI類,方便加載圖片,提供PlaceHolder功能。

-(void)load:(NSString *)image;

以imageView自身尺寸*screenScale作為參考尺寸,加載image圖片。在加載圖片時使用默認的placeHolder。

-(void)load:(NSString *)image placeHolder:(NSString *)placeHolder;

以imageView自身尺寸*screenScale作為參考尺寸,加載image圖片。在加載圖片時使用指定的placeHolder。

-(void)load:(NSString *)image width:(float) w height:(float)h;

以w*h為參考尺寸加載image圖片。使用默認的placeHolder。

-(void)load:(NSString *)image width:(float) w height:(float)h placeHolder:(NSString *)placeHolder;

以w*h為參考尺寸加載image圖片。使用指定的placeHolder。

-(void)loadOriginal:(NSString *)image;

加載原始圖片。使用默認的placeHolder。

-(void)loadOriginal:(NSString *)image placeHolder:(NSString *)placeHolder;

加載原始圖片。使用指定的placeHolder。

DotCIntegrateAdapter

集成適配器,DotCImageManager需要主體項目提供一些必要功能才能正常工作

- (void) request:(NSString*)image width:(int)w height:(int)h info:(void*)info;

DotCImageManager需要請求圖片時會調用該接口。
image : 圖片ID
w : 參考尺寸寬度
h : 參考尺寸高度
info : 請求額外信息,在調用onRequest:info中需要

- (UIImage*)  getPlaceHolder:(NSString*)name;

DotCImageView在使用placeHolder時會調用該接口獲取圖片。

- (NSString*) getDatabasePath;

DotCImageManager使用的數據庫路徑。

- (int) getMaxMemoryCacheSize;

DotCImageManager使用的最大內存緩存大小。

- (void) onRequest:(NSData*)imageData info:(void*)info;

當request:width:height:info請求圖片成功后者失敗后,必須通過調用該接口回調通知DotCImageManager。

項目主頁:http://www.baiduhome.net/lib/view/home/1446218430726

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!