SDWebImage

SDWebImage主要結(jié)構(gòu)

  • 主要類


    SDWebImage主要結(jié)構(gòu)
  • 下載圖片的流程


    下載圖片流程

1. 基本使用

下載策略

下載策略

1.1 SDWebManager類

  • 只需要簡單獲得一張圖片時,使用下面的方法,也會進行磁盤&內(nèi)存緩存
    [[SDWebImageManager sharedManager] loadImageWithURL:_url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        <#code#>
    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        <#code#>
    }];

1.2 SDWebManagerDownload類

  • 只需要下載圖片
  • 注意completed是在子線程中,如果需要在completed更新UI,需要進行線程間通信。
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:_url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        <#code#>
    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {

        [NSOperationQueue mainQueue]addOperationWithBlock:^{
           //在這里更新UI
        }

    }];

1.3 UIImageView分類

  • 在下載圖片且需要知道下載進度時,一般用下面這個函數(shù)。
  • 在completed回調(diào)中,SDImageCacheType可以知道緩存的策略,有SDImageCacheTypeNone=直接下載,SDImageCacheTypeDisk=磁盤緩存,SDImageCacheTypeMemory=內(nèi)存緩存,三種策略。
    /*
     第1個參數(shù):下載圖片的url
     第2個參數(shù):占位圖片
     第3個參數(shù):下載策略
     第4個參數(shù):progress 進度回調(diào)
                receivedSize:已經(jīng)下載圖片的數(shù)據(jù)大小
                expectedSize:圖片總共的數(shù)據(jù)大小
     第4個參數(shù):completed 完成回調(diào)
                image:要下載的圖片
                error:錯誤信息
                cacheType:緩存類型(0代表從互聯(lián)網(wǎng)下載,1代表從磁盤獲得,2代表從內(nèi)存獲得)
                imageURL:圖片url
     */
   [_image sd_setHighlightedImageWithURL:url 
                                 options:[UIImage imageNamed:@"1"] 
                                progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) 
    {
                                NSLog(@"%ld,%ld,%@",(long)receivedSize,(long)expectedSize,targetURL);
    } 
                               completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) 
    {
                                NSLog(@"%@,%@,%ld",image,error,(long)cacheType);
                                [self.image setImage:image];
    }];

內(nèi)存警告

  • 在AppDelegate中處理內(nèi)存警告
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    //1.清空緩存
    //clearDisk:直接刪除文件夾,然后重新創(chuàng)建
    //cleadDisk:清除過期緩存(7天),計算當(dāng)前緩存大小,和設(shè)置的最大緩存數(shù)量比較,若果超出那么會繼續(xù)刪除(按照文件創(chuàng)建的先后順序刪除)
    [[SDWebImageManager sharedManager].imageCache clearWithCacheType:0 completion:^{
        <#code#>
    }];
    //2.取消當(dāng)前所有操作
    [[SDWebImageManager sharedManager] cancelAll];
    
}

NSCache

1.NSCache是專門用來進行緩存處理的

  • NSCache是蘋果提供的官方緩存類,具體使用和NSDictionary相似,在AFN和SDWebImage中來管理緩存
  • 官方解釋NSCache在系統(tǒng)內(nèi)存很低時,會自動釋放對象(模擬器不會)
    建議:接收到內(nèi)存警告時主動調(diào)用removeAllObjective方法釋放對象
  • NSCache是線程安全的,在多線程操作中,不需要對NSCache加鎖
  • NSCache的key只是對對象進行Strong引用,不是拷貝

2.NSCache的屬性

  • name:名稱
  • delegate:代理
  • totalCostLimit:緩存空間最大成本,超出上限會自動回收對象,默認(rèn)值0表示沒限制
  • countLimit:能夠緩存的對象的最大數(shù)量,默認(rèn)值0
  • evictsObjectsWithDiscardedContent:標(biāo)識緩存是否回收廢棄內(nèi)容

3.NSCache的方法

[func setObject(ObjectType, forKey: KeyType)]
Sets the value of the specified key in the cache.

[func setObject(ObjectType, forKey: KeyType, cost:Int )]
Sets the value of the specified key in the cache, and associates the key-value pair with the specified cost.

[func removeObject(forKey: KeyType)]
Removes the value of the specified key in the cache.

[func removeAllObjects()]
Empties the cache.

其他

  • SDWebImage下載的最大并發(fā)數(shù):6
  • SDWebImage隊列中任務(wù)的處理方式:默認(rèn)FIFO
  • SDWebImage磁盤緩存的文件名稱:圖片url地址MD5加密后的字符串作為圖片名稱

mac進行MD5加密的方法:命令行echo -n "http://...../..../... .jpeg" |md5

  • SDWebImage對內(nèi)存警告的處理方式?:

對系統(tǒng)的通知方法進行監(jiān)聽,當(dāng)接收到內(nèi)存警告會清理內(nèi)存,當(dāng)接收到用戶點擊Home鍵會清理磁盤,當(dāng)接收到即將進入后臺會清理后臺內(nèi)存

  • SDWebImage進行緩存處理的方式:

可變字典 ——> NSCache

  • SDWebImage如何判斷圖片類型:

獲得傳入圖片的第一個字節(jié),在判斷圖片類型時,匹配第一個字節(jié),0xFF是jpeg,0x89是png,0x47是gif...

  • SDWebImage如何下載圖片?

通過網(wǎng)絡(luò)請求NSURLSession請求數(shù)據(jù),如果數(shù)據(jù)較大,會多次接收數(shù)據(jù),拼接成一個完整數(shù)據(jù)

  • 請求超時怎么辦?

超時時間15.0s,

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容