第三方庫:SDWebImage簡單使用

官方接口文檔地址:http://cocoadocs.org/docsets/SDWebImage/3.8.1/緩存

SDWebImage 提供了一個支持網絡下載圖片的UIImageView的分類,主要有如下功能:服務器

  • 爲Cocoa Touch框架提供一個UIImageView的分類,加載圖片並進行緩存處理。
  • 異步圖像下載
  • 異步存儲器+具有自動緩存過時處理的磁盤映像緩存
  • 支持GIF播放
  • 支持WebP格式
  • 背景圖像解壓縮
  • 保證同一個url圖片資源不被屢次下載
  • 保證錯誤url不被反覆嘗試下載
  • 保證不會阻塞主線程
  • 高性能
  • 使用GCD和ARC
  • 支持Arm64架構

 

SDWebImage主要的類和類別:網絡

  • SDWebImageManager :管理圖片下載和緩存;
  • SDWebImageDownloader :負責圖片的下載:
  • SDImageCache:負責圖片緩存;
  • UIButton+WebCache :設置UIButton的圖片
  • UIImageView+WebCache:設置UIImageView的圖片;
  • NSData+ImageContentType:獲取圖片的類型,image/jpeg或者 image/gif

 

UIImageView+WebCache 基本使用

//導入頭文件:#import "UIImageView+WebCache.h"
    
    /**
     *  如下方法中,程序會異步下載圖片,並自動緩存到內存和本地;
     */
    
    /**
     *  方法1:sd_setImageWithURL:url placeholderImage:image
     *  url: 圖片地址
     *  image:圖片沒有下載時顯示的佔位圖片
     */
    [self.imageView sd_setImageWithURL:url placeholderImage:image];
    
    
    /**
     *  方法2:帶下載進度和結束處理的block
     *  url: 圖片地址
     *  image:圖片沒有下載時顯示的佔位圖片
     *  options:圖片下選項
     *  progress:能夠在這設置圖片下載進度的顯示, receivedSize:已接收大小;expectedSize:圖片總大小;
     *  completed:圖片下載完成的處理,image:下載的圖片;error:錯誤碼;
     *  cacheType:緩存的類型
        SDImageCacheTypeNone:沒有使用緩存,圖片是直接下載的
        SDImageCacheTypeDisk:磁盤緩存
        SDImageCacheTypeMemory:內存緩存
        緩存處理:內存緩存+磁盤緩存
     *  imageURL:圖片的url
     */
    [self.imageView sd_setImageWithURL:url placeholderImage:image options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        //設置進度條
        [self.progressView setProgress:(1.0 * receivedSize / expectedSize)];
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    
    
//取消當前圖片下載
- (void)sd_cancelCurrentImageLoad
- (void)sd_cancelCurrentAnimationImagesLoad

//獲取當前圖片URL
- (NSURL *)sd_imageURL

//下載一組圖片,而且以動畫的形式展示
- (void)sd_setAnimationImagesWithURLs:(NSArray *)arrayOfURLs

//是否顯示等待菊花
- (void)setShowActivityIndicatorView:(BOOL)show
//菊花的類型
- (void)setIndicatorStyle:(UIActivityIndicatorViewStyle)style

 

SDWebImageManager

 DWebImageManager負責圖片的緩存和下載,其餘分類下載圖片也是調用的這個類;架構

//頭文件: "SDWebImageManager.h"
    
    //屬性:
    @property (weak, nonatomic) id <SDWebImageManagerDelegate> delegate;    //代理
    @property (strong, nonatomic, readonly) SDImageCache *imageCache;   //圖片緩存管理
    @property (strong, nonatomic, readonly) SDWebImageDownloader *imageDownloader;  //圖片下載
    @property (nonatomic, copy) SDWebImageCacheKeyFilterBlock cacheKeyFilter; //將圖片的URL變成緩存圖片的關鍵字

    [[SDWebImageManager sharedManager] setCacheKeyFilter:^(NSURL *url) { url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path]; return [url absoluteString]; }];
    
    //方法:
//    + (SDWebImageManager *)sharedManager; //單例方法
    SDWebImageManager *mamager = [SDWebImageManager sharedManager];

    //二、執行下載,具體參數和上面的函數一致;
    [mamager downloadImageWithURL:url options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
       
    }];
    
    //根據URL返回緩存圖片的鍵值
    - (NSString *)cacheKeyForURL:(NSURL *)url
    
     //判斷圖片是否已經被緩存
    - (BOOL)cachedImageExistsForURL:(NSURL *)url
    
    //異步檢查圖片是否已經緩存,completionBlock一般在主線程中執行
    - (void)cachedImageExistsForURL:(NSURL *)url completion:(SDWebImageCheckCacheCompletionBlock)completionBlock
    
    //根據URL緩存圖片
    - (void)saveImageToCache:(UIImage *)image forURL:(NSURL *)url;
    
    //取消全部當前操做
    - (void)cancelAll
    
    //檢查是否已經緩存在本地
    - (BOOL)diskImageExistsForURL:(NSURL *)url
    
    //異步檢查圖片是否已經緩存在本地
    - (void)diskImageExistsForURL:(NSURL *)url completion:(SDWebImageCheckCacheCompletionBlock)completionBlock
    
    //是否有操做在運行
    - (BOOL)isRunning

SDWebImageManagerDelegate

//返回YES:當圖片不存在於緩存中時,根據圖片URL下載圖片;
//返回NO:當圖片不存在於緩存中時,不下載圖片;
- (BOOL)imageManager:(SDWebImageManager *)imageManager shouldDownloadImageForURL:(NSURL *)imageURL

//    圖片下載以後,當即進行轉換,而後再存入緩存中。注:必須在全局隊列中執行;
//    - (UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL

 SDImageCache

SDImageCache維護內存管理和可選的磁盤管理;寫磁盤緩存操做是異步執行的,不會影響UI;併發

屬性:

//圖片在內存中保存的最大時間,單位:秒
@property (assign, nonatomic) NSInteger maxCacheAge

//在內存中可以存儲圖片的最大容量,單位:字節
@property (assign, nonatomic) NSUInteger maxCacheSize

//保存在存儲器中像素的總和
@property (assign, nonatomic) NSUInteger maxMemoryCost

//內存中能緩存對象的最大個數
@property (assign, nonatomic) NSUInteger maxMemoryCountLimit

//是否使用內存緩存,默認是YES
@property (assign, nonatomic) BOOL shouldCacheImagesInMemory

//將下載的圖片解壓而且緩存能夠提升性能,同時也會消耗一些內存。默認是YES.
//若是有內存消耗致使崩潰的現象,設成NO;
@property (assign, nonatomic) BOOL shouldDecompressImages

//禁止雲備份,默認是YES;
@property (assign, nonatomic) BOOL shouldDisableiCloud

 

類方法:
+ (SDImageCache *)sharedImageCache

實例方法:
//添加只讀的緩存路徑查找SDImageCache預先緩存的圖片。若是你想在APP綁定預加載圖片,這個函數會有幫助;
- (void)addReadOnlyCachePath:(NSString *)path

//經過鍵值獲取緩存路徑,
//path:緩存路徑的根目錄
- (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path
Discussion

//異步計算磁盤緩存的大小
- (void)calculateSizeWithCompletionBlock:(SDWebImageCalculateSizeBlock)completionBlock
Discussion

//清理全部!!過時的 !!磁盤緩存
- (void)cleanDisk

//清理全部!!過時的 !!磁盤緩存,block爲空時,函數會馬上返回,
- (void)cleanDiskWithCompletionBlock:(SDWebImageNoParamsBlock)completionBlock

//清理全部磁盤緩存圖片,不分過時不過時;
- (void)clearDisk

//清理全部磁盤緩存圖片,不分過時不過時;block爲空時,函數會馬上返回,
- (void)clearDiskOnCompletion:(SDWebImageNoParamsBlock)completion
Discussion

//清理全部內存緩存
- (void)clearMemory

//獲取制定鍵值默認的緩存路徑
- (NSString *)defaultCachePathForKey:(NSString *)key

//檢查圖片是否已經緩存在磁盤上;
- (BOOL)diskImageExistsWithKey:(NSString *)key

//異步檢查圖片是否已經緩存在磁盤上, completion block必須在主隊列執行;
- (void)diskImageExistsWithKey:(NSString *)key completion:(SDWebImageCheckCacheCompletionBlock)completionBlock

//獲取磁盤緩存的圖片的數量
- (NSUInteger)getDiskCount

//獲取用做磁盤緩存的大小
- (NSUInteger)getSize

//根據鍵值查找圖片,先在內存緩存中找,而後再磁盤中找
- (UIImage *)imageFromDiskCacheForKey:(NSString *)key
Query the disk cache synchronously after checking the memory cache.

//在內存緩存中查找圖片
- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key

//根據命名空間初始化一個新的緩存存儲空間,
- (id)initWithNamespace:(NSString *)ns

//根據命名空間和緩存路徑初始化一個新的緩存存儲空間,
- (id)initWithNamespace:(NSString *)ns diskCacheDirectory:(NSString *)directory

//根據路徑建立磁盤緩存目錄
- (NSString *)makeDiskCachePath:(NSString *)fullNamespace
 
//異步查詢磁盤緩存
- (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock

//移除內存和磁盤中鍵值對應的圖片
- (void)removeImageForKey:(NSString *)key

//異步刪除內存和磁盤中的圖片,磁盤刪除是可選的;
- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk

//異步刪除內存和磁盤中的圖片,磁盤刪除是可選的,帶completion block
- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(SDWebImageNoParamsBlock)completion

//異步刪除內存和磁盤中的圖片,帶completion block
- (void)removeImageForKey:(NSString *)key withCompletion:(SDWebImageNoParamsBlock)completion

//根據鍵值將圖片存儲在內存和磁盤中;
- (void)storeImage:(UIImage *)image forKey:(NSString *)key

//根據鍵值將圖片存儲在內存和磁盤中,磁盤存儲是可選的;
- (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk

//根據鍵值將圖片存儲在內存和磁盤中,磁盤存儲是可選的;
//recalculate:直接使用imageData仍是從image中獲取;
//imageData:服務器返回的數據,將會用做磁盤存儲;爲了節約性能和CPU,一般不會將image轉換成可存儲、壓縮過的圖片格式;
- (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk

//緩存圖片數據到本地磁盤
- (void)storeImageDataToDisk:(NSData *)imageData forKey:(NSString *)key

SDWebImageDownloader 

//屬性

// 當前須要下載的數量
@property (readonly, nonatomic) NSUInteger currentDownloadCount

//下載超時時間,默認15S
@property (assign, nonatomic) NSTimeInterval downloadTimeout

//下載隊列操做順序,默認是SDWebImageDownloaderFIFOExecutionOrder
@property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder

//獲取下載圖片的HTTP請求的請求頭;
@property (nonatomic, copy) SDWebImageDownloaderHeadersFilterBlock headersFilter

//最大併發下載數;
@property (assign, nonatomic) NSInteger maxConcurrentDownloads 

//密碼
@property (strong, nonatomic) NSString *password

//對下載的圖片壓縮並緩存能夠提升性能,同時也會消耗內存。默認是YES;
//若是內存消耗過多引發崩潰,設成NO;
@property (assign, nonatomic) BOOL shouldDecompressImages

//URL請求的證書
@property (strong, nonatomic) NSURLCredential *urlCredential

//用戶名
@property (strong, nonatomic) NSString *username

 

//類方法 
+ (SDWebImageDownloader *)sharedDownloader
Discussion

//實例方法
//取消隊列中全部下載操做
- (void)cancelAllDownloads

//根據URL建立異步下載
- (id<SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock
Discussion

//建立下載隊列
- (void)setOperationClass:(Class)operationClass
Sets a subclass of SDWebImageDownloaderOperation as the default NSOperation to be used each time SDWebImage constructs a request operation to download an image.

//暫停下載
- (void)setSuspended:(BOOL)suspended
Discussion

//爲每個http請求添加http請求頭
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field

//http請求頭數據
- (NSString *)valueForHTTPHeaderField:(NSString *)field
相關文章
相關標籤/搜索