天天看點

使用開源庫 SDWebImage 異步下載下傳緩存圖檔(持續更新)

使用開源庫 SDWebImage 異步下載下傳緩存圖檔(持續更新)
使用開源庫 SDWebImage 異步下載下傳緩存圖檔(持續更新)

asynchronous image downloader with cache support with an uiimageview category

uiimageview的類目,支援異步圖檔下載下傳,支援緩存機制

this library provides a category for uiimageview with support for remote images coming from the web.

這個庫給uiimageview提供類目,支援遠端下載下傳圖檔(從網絡上)

it provides:

an uiimageview category adding web image and cache management to the cocoa touch framework

an asynchronous image downloader

an asynchronous memory + disk image caching with automatic cache expiration handling

animated gif support

webp format support

a background image decompression

a guarantee that the same url won't be downloaded several times

a guarantee that bogus urls won't be retried again and again

a guarantee that main thread will never be blocked

performances!

use gcd and arc

arm64 support

一個uiimageview的類目,給 cocoa touch 架構添加了異步下載下傳遠端圖檔以及管理圖檔緩存的功能

一個圖檔的異步下載下傳器

一個記憶體 + 磁盤的緩存機制,并自動管理

gif動畫支援

webp格式支援

背景解壓圖檔

確定同樣地 url 不會重複的下載下傳多次

確定無效的 url 不會重複的連結

確定主線程永遠不會阻塞

效果拔群!

使用gcd以及要求arc

支援64位系統

以下進行sdwebimage使用的教程解說.

2. 頭檔案較多,請建立一個 sdwebimage.h 的頭檔案,寫以下代碼并包含所有頭檔案,添加到.pch檔案中

-------------------------------------------------------------------------------

//mkannotationview地圖的注解view緩存

#import "mkannotationview+webcache.h"

//判斷nsdata是否什麼類型的圖檔(例如:jpg,png,gif)

#import "nsdata+imagecontenttype.h"

//是sdwebimage包的一部分

#import "sdimagecache.h"      //緩存相關

#import "sdwebimagecompat.h"  //元件相關

#import "sdwebimagedecoder.h" //解碼相關

//圖檔下載下傳以及下載下傳管理器

#import "sdwebimagedownloader.h"

#import "sdwebimagedownloaderoperation.h"

//管理以及操作

#import "sdwebimagemanager.h"

#import "sdwebimageoperation.h"

//uibutton類目

#import "uibutton+webcache.h"

//gif類目

#import "uiimage+gif.h"

//圖檔其他類目

#import "uiimage+multiformat.h"

#import "uiimage+webp.h"

#import "uiimageview+webcache.h"

3. 正式開始講解怎麼使用

獨立的下載下傳圖檔的功能(沒有緩存機制)

分析:此方法為單例模式,看其源碼

+ (sdwebimagedownloader *)shareddownloader {

    static dispatch_once_t once;

    static id instance;

    dispatch_once(&once, ^{

        instance = [self new];

    });

    return instance;

}

- (id)init {

    if ((self = [super init])) {

        _executionorder = sdwebimagedownloaderfifoexecutionorder;

        _downloadqueue = [nsoperationqueue new];

        _downloadqueue.maxconcurrentoperationcount = 2;

        _urlcallbacks = [nsmutabledictionary new];

        _httpheaders = [nsmutabledictionary dictionarywithobject:@"image/webp,image/*;q=0.8" forkey:@"accept"];

        _barrierqueue = dispatch_queue_create("com.hackemist.sdwebimagedownloaderbarrierqueue", dispatch_queue_concurrent);

        _downloadtimeout = 15.0;

    }

    return self;

typedef ns_enum(nsinteger, sdwebimagedownloaderexecutionorder) {

    /**

     * default value. all download operations will execute in queue style (first-in-first-out). 預設值.所有的下載下傳操作将會進入串行線程池fifo

     */

    sdwebimagedownloaderfifoexecutionorder,

     * all download operations will execute in stack style (last-in-first-out).

    sdwebimagedownloaderlifoexecutionorder

};

如果僅僅看上面的部分,知道下載下傳單例由串行線程池管理着,按照隊列執行,一次最多能執行兩個,但我在實際測試過程中發現,并不像描述的那樣子......,好吧,就當做是并發執行的了(此處疑問有時間再解決)

獨立的下載下傳圖檔的功能(有緩存機制)

清除緩存檔案

判斷本地緩存中是否存在網絡中的圖檔

擷取緩存圖檔張數

擷取所有緩存圖檔的總大小

直接從緩存中提取圖檔

直接删除緩存中得圖檔

在uitableview中使用

-未完待續-