SDWebImage基本使用和內部細節

SDWebImage基本使用

// 引入相關頭文件
#import "UIImageView+WebCache.h"
#import "SDWebImageManager.h"
#import "SDWebImageDownloader.h"
#import "UIImage+GIF.h"

// 1.下載圖片且可以獲取下載進度,內存緩存&磁盤緩存
-(void)download
{
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:@"imagUrl"] placeholderImage:[UIImage imageNamed:@"placeholderName"] options:SDWebImageCacheMemoryOnly | SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {

    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        switch (cacheType) {
            case SDImageCacheTypeNone:
                NSLog(@"直接下載");
                break;
            case SDImageCacheTypeDisk:
                NSLog(@"磁盤緩存");
                break;
            case SDImageCacheTypeMemory:
                NSLog(@"內存緩存");
                break;
            default:
                break;
        }
    }];

    // 存放目錄
    NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]);

}

// 2.只需要簡單獲得一張圖片,可不設置顯示,內存緩存&磁盤緩存
-(void)download2
{
    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:@"imagUrl"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"%f",1.0 * receivedSize / expectedSize);

    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {

        // 得到圖片
        self.imageView.image = image;
    }];
}

// 3.沒有做任何緩存處理
-(void)download3
{
    // data:圖片的二進制數據
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:@"imagUrl"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {

    } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {

        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
             self.imageView.image = image;
        }];

    }];
}

// 4.播放Gif圖片
-(void)gif
{   
    UIImage *image = [UIImage sd_animatedGIFNamed:@"gifImagName"];
    self.imageView.image = image;
}

SDWebImage內部細節

// 內存警告
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    // 1.清空緩存
    // clearDisk方法:直接刪除緩存目錄下面的文件,然後重新創建空的緩存文件
    // cleanDisk方法:清除過期緩存,計算當前緩存的大小,和設置的最大緩存數量比較,如果超出那麼會繼續刪除(按照文件了創建的先後順序)
    // 默認過期時間:7天
    [[SDWebImageManager sharedManager].imageCache clearMemory];

    // 2.取消當前所有的操作
    [[SDWebImageManager sharedManager] cancelAll];

    // 3.圖片下載最大併發數量  6個
    _downloadQueue.maxConcurrentOperationCount = 6;

    // 4.緩存文件的保存名稱如何處理? 
    //   拿到圖片的URL路徑,對該路徑進行MD5加密

    // 5.該框架內部對內存警告的處理方式? 
    //   內部通過監聽UIApplicationDidReceiveMemoryWarning通知的方式清除緩存

    // 6.該框架進行緩存處理的方式:使用NSCache(和字典的用法差不多)

    // 7.如何判斷圖片的類型: 在判斷圖片類型的時候,只匹配第一個字節
    // #import "NSData+ImageContentType.h"
    -(void)type
    {
        NSData *imageData = [NSData dataWithContentsOfFile:@"imagePath.png"];
        NSString *typeStr = [NSData sd_contentTypeForImageData:imageData];
        NSLog(@"%image type = @", typeStr);
    }

    // 8.隊列中任務的處理方式:FIFO

    // 9.如何下載圖片的? 
    // 發送網絡請求下載圖片, 老版本SDWebImage使用NSURLConnection,新版本使用的是NSURSession
    // 使用它們的代理方法如: didReveiveData方法,回調顯示下載結果和下載進度

    // 10.默認請求超時的時間: 15秒    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章