亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

SDWebImage的簡單使用

2019-11-14 17:54:32
字體:
來源:轉載
供稿:網友

一、前言

  第一次使用SDWebImage是在自己做《康復醫生》的時候。記得那時有一個表格要顯示所有的用戶頭像。不使用它的時候每次加載該table都會很卡頓,后來使用后發現不卡頓了。瞬間感覺這個世界好有愛。

二、安裝

  首先,SDWebImage的git地址是:https://github.com/rs/SDWebImage。我們可以直接到這里進行下載,然后添加到自己的項目中去。

三、使用場景(前提是已經導入了SDWebImage這個庫)

1、場景一、加載圖片

    使用SDWebImage可以去加載遠程圖片,而且還會緩存圖片,下次請求會看一下是否已經存在于緩存中,如果是的話直接取本地緩存,如果不是的話則重新請求。使用方法很簡單,在需要使用該場景的類中導入

#import "UIImageView+WebCache.h"

然后調用:

- (void)sd_setImageWithPReviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;

即可。

  我們還可以在UIImageView+WebCache.h中看到其他的方法,和上邊的方法類似,讀者自行查看即可。

2、場景二、使用它做本地緩存

  很多時候我們可能拍照得到的一張圖片要多個地方使用,那么我們就希望可以把這張圖片放到緩存里面,然后每次用這張圖片的時候就去通過特定的方式取即可。SDWebImage就有這樣的一個類:SDImageCache。該類完美地幫助了我們解決了這個問題。

  使用的時候,我們首先要在使用的類里面做導入:

#import "SDImageCache.h"

然后就可以進行相關的操作了。讓我們直接看看SDImageCache.h文件:

@property (assign, nonatomic) NSUInteger maxMemoryCost;/** * The maximum length of time to keep an image in the cache, in seconds */@property (assign, nonatomic) NSInteger maxCacheAge;/** * The maximum size of the cache, in bytes. */@property (assign, nonatomic) NSUInteger maxCacheSize;/** * Returns global shared cache instance * * @return SDImageCache global instance */+ (SDImageCache *)sharedImageCache;/** * Init a new cache store with a specific namespace * * @param ns The namespace to use for this cache store */- (id)initWithNamespace:(NSString *)ns;/** * Add a read-only cache path to search for images pre-cached by SDImageCache * Useful if you want to bundle pre-loaded images with your app * * @param path The path to use for this read-only cache path */- (void)addReadOnlyCachePath:(NSString *)path;/** * Store an image into memory and disk cache at the given key. * * @param image The image to store * @param key   The unique image cache key, usually it's image absolute URL */- (void)storeImage:(UIImage *)image forKey:(NSString *)key;/** * Store an image into memory and optionally disk cache at the given key. * * @param image  The image to store * @param key    The unique image cache key, usually it's image absolute URL * @param toDisk Store the image to disk cache if YES */- (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk;/** * Store an image into memory and optionally disk cache at the given key. * * @param image       The image to store * @param recalculate BOOL indicates if imageData can be used or a new data should be constructed from the UIImage * @param imageData   The image data as returned by the server, this representation will be used for disk storage *                    instead of converting the given image object into a storable/compressed image format in order *                    to save quality and CPU * @param key         The unique image cache key, usually it's image absolute URL * @param toDisk      Store the image to disk cache if YES */- (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk;/** * Query the disk cache asynchronously. * * @param key The unique key used to store the wanted image */- (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock;/** * Query the memory cache synchronously. * * @param key The unique key used to store the wanted image */- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key;/** * Query the disk cache synchronously after checking the memory cache. * * @param key The unique key used to store the wanted image */- (UIImage *)imageFromDiskCacheForKey:(NSString *)key;/** * Remove the image from memory and disk cache synchronously * * @param key The unique image cache key */- (void)removeImageForKey:(NSString *)key;/** * Remove the image from memory and disk cache synchronously * * @param key             The unique image cache key * @param completionBlock An block that should be executed after the image has been removed (optional) */- (void)removeImageForKey:(NSString *)key withCompletition:(void (^)())completion;/** * Remove the image from memory and optionally disk cache synchronously * * @param key      The unique image cache key * @param fromDisk Also remove cache entry from disk if YES */- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk;/** * Remove the image from memory and optionally disk cache synchronously * * @param key             The unique image cache key * @param fromDisk        Also remove cache entry from disk if YES * @param completionBlock An block that should be executed after the image has been removed (optional) */- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletition:(void (^)())completion;/** * Clear all memory cached images */- (void)clearMemory;/** * Clear all disk cached images. Non-blocking method - returns immediately. * @param completionBlock An block that should be executed after cache expiration completes (optional) */- (void)clearDiskOnCompletion:(void (^)())completion;/** * Clear all disk cached images * @see clearDiskOnCompletion: */- (void)clearDisk;/** * Remove all expired cached image from disk. Non-blocking method - returns immediately. * @param completionBlock An block that should be executed after cache expiration completes (optional) */- (void)cleanDiskWithCompletionBlock:(void (^)())completionBlock;/** * Remove all expired cached image from disk * @see cleanDiskWithCompletionBlock: */- (void)cleanDisk;/** * Get the size used by the disk cache */- (NSUInteger)getSize;/** * Get the number of images in the disk cache */- (NSUInteger)getDiskCount;/** * Asynchronously calculate the disk cache's size. */- (void)calculateSizeWithCompletionBlock:(void (^)(NSUInteger fileCount, NSUInteger totalSize))completionBlock;/** * Check if image exists in cache already */- (BOOL)diskImageExistsWithKey:(NSString *)key;/** *  Get the cache path for a certain key (needs the cache path root folder) * *  @param key  the key (can be obtained from url using cacheKeyForURL) *  @param path the cach path root folder * *  @return the cache path */- (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path;/** *  Get the default cache path for a certain key * *  @param key the key (can be obtained from url using cacheKeyForURL) * *  @return the default cache path */- (NSString *)defaultCachePathForKey:(NSString *)key;

  不要看著想吐就行。先看看使用吧。

 存圖片:

 SDImageCache *imageCache = [SDImageCache sharedImageCache];

   [imageCache storeImage:image forKey:@"myphoto" toDisk:YES];

取圖片:

 SDImageCache *imageCache = [SDImageCache sharedImageCache];    UIImage *image = [imageCache imageFromDiskCacheForKey:@"myphoto"];

這樣就可以取到自己存的圖片了??梢钥匆幌耂DImageCache.h這個類了,里面提供了存圖片到內存和磁盤的方法,還有取圖片的方法,以及判斷該圖片是否存在的方法。還有就是刪除圖片、清空磁盤緩存、得到緩存大小等方法。

場景三、做設置中的清除緩存功能

  簡單來說,當我們點擊清除緩存按鈕的時候會觸發的消息如下:

- (void)clearCaches{    [MBProgressHUD showMessage:@"正在清理.."];        [[SDImageCache sharedImageCache] clearMemory];    [[SDImageCache sharedImageCache] clearDisk];    [self performSelectorOnMainThread:@selector(cleanCacheSuccess) withObject:nil waitUntilDone:YES];}

- (void)cleanCacheSuccess

{

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{

        self.cacheLab.text = @"0.00M";

        return;

   });

}

很簡單,就是把使用SDWebImage緩存的圖片都清空就可以了,這里包括了內存中的緩存圖片以及磁盤上的緩存圖片。

 場景四、一個APP多用戶緩存存儲

  比如一個用戶下面有好多只屬于個人的圖片,那么如果我們想做到當切換用戶再切換當前用戶還可以看到原來用戶的圖片時,就可以用到SDImageCache的命名空間功能。其實很簡單

 1、簡單說一下:當我們使用SDImageCache去緩存圖片的時候,如果我們是這樣初始化的SDImageCache:

    SDImageCache *image = [SDImageCache sharedImageCache];

那么我們會在Caches(沙盒)文件夾下看到一個com.hackemist.SDWebImageCache.default文件夾,這里就是用來存儲sharedImageCache初始化的緩存存儲的圖片。如果我們是這樣初始化SDImageCache:

  SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"TTTTTT"];

那么我們會在Caches文件夾下看到一個com.hackemist.SDWebImageCache.TTTTT文件夾。這里就是用來存儲這樣初始化的緩存存儲的圖片。

其他看一下源碼可以知道,它是根據命名空間來命名文件夾的。

這樣我們就可以用用戶名作為命名空間名稱,這樣每次取緩存都用用戶名就OK了。

還有其他的使用場景,這里就不再贅述了。

四、實現原理 

 其實SDWebImage之所以能夠實現緩存的原理關鍵就是在哪個key值。

  比如我們在使用

- (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;

的時候,其實就是把url當做了一個圖片的key值,然后存儲對應的圖片,如果下次請求的url和這次請求的url一樣,那么就直接根據url(這個key)來取圖片,如果url作為key的圖片緩存不存在,就去請求遠程服務器,然后請求過來之后再次將url和圖片對應,然后存儲。

 

五、總結

 

六、其他

  未完待續。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久成人精品电影| 欧美视频在线观看 亚洲欧| 亚洲午夜国产成人av电影男同| 欧美日韩爱爱视频| 欧美精品久久久久a| 亚洲男人天堂2023| 日韩在线视频中文字幕| 欧美一区二区三区……| 91精品国产综合久久久久久久久| 精品亚洲精品福利线在观看| 91精品国产色综合久久不卡98| 国产精品国产亚洲伊人久久| 在线国产精品视频| 久久久久久久久久久亚洲| 亚洲色图国产精品| 一区二区在线视频| 日韩中文字幕第一页| 国产成人精品免高潮在线观看| 国产精品视频久久久久| 亚洲欧美国产精品| 久久精品视频在线播放| 成人一区二区电影| 亚洲人成亚洲人成在线观看| 91影视免费在线观看| 欧美日韩在线免费观看| 久久成人18免费网站| 亚洲美女av在线| 久久精品在线视频| 日韩av中文字幕在线免费观看| 国产精品毛片a∨一区二区三区|国| 日韩电影在线观看中文字幕| 国产日韩欧美综合| 国产精品扒开腿做爽爽爽的视频| 亚洲欧美另类自拍| 欧美大学生性色视频| 一区二区成人av| 久久久久久久影视| 欧美成人激情图片网| 精品亚洲一区二区三区四区五区| 亚洲欧美综合精品久久成人| 欧美天堂在线观看| 欧美黑人视频一区| 91福利视频在线观看| 久久久久久91香蕉国产| 色香阁99久久精品久久久| 97超视频免费观看| 国产女人18毛片水18精品| 欧美成人午夜影院| 最近日韩中文字幕中文| 久久噜噜噜精品国产亚洲综合| 91禁外国网站| 国产成人精品一区二区| 久久久久久国产精品三级玉女聊斋| 欧美性猛交xxxx黑人猛交| 日本最新高清不卡中文字幕| 日韩av电影在线免费播放| 欧美孕妇性xx| 亚洲国产精品成人一区二区| 国产91亚洲精品| 国产欧美一区二区三区在线| 欧美激情伊人电影| 97视频在线观看免费高清完整版在线观看| 国产亚洲免费的视频看| 国产黑人绿帽在线第一区| 韩国三级日本三级少妇99| 国产精品国语对白| 国产精品美女免费视频| 久久久免费观看| 97在线视频精品| 欧美性感美女h网站在线观看免费| 欧美黑人xxxx| 国产欧美日韩免费看aⅴ视频| 国产亚洲福利一区| 欧美性感美女h网站在线观看免费| 久久精品视频va| 精品国产91乱高清在线观看| 成人做爽爽免费视频| 91久久久久久久一区二区| 亚洲激情中文字幕| 日韩在线中文字| 欧美一区在线直播| 亚洲精品资源在线| 亚洲电影免费观看高清完整版在线观看| 国产午夜精品一区二区三区| 欧美另类极品videosbest最新版本| 国产精品视频精品| 国产欧美精品日韩| 成人av在线天堂| 久久久久久亚洲精品中文字幕| 欧洲亚洲免费在线| 成人在线中文字幕| 国产91精品久| 国产欧美精品va在线观看| 国产精品精品一区二区三区午夜版| 久久久久久久999精品视频| 亚洲精品一区久久久久久| 色综合天天狠天天透天天伊人| 国产999精品久久久| 久久伊人免费视频| 色一情一乱一区二区| 91香蕉嫩草影院入口| 2019亚洲男人天堂| 欧美极品在线播放| 日产精品久久久一区二区福利| 亚洲在线视频观看| 国产日产欧美精品| 在线视频日本亚洲性| 亚洲精品国精品久久99热一| 成人写真福利网| 欧美国产高跟鞋裸体秀xxxhd| y97精品国产97久久久久久| 欧美日韩国产精品专区| 在线免费观看羞羞视频一区二区| 久久国产精品电影| 欧美极品少妇xxxxⅹ裸体艺术| 成人国产亚洲精品a区天堂华泰| 51精品国产黑色丝袜高跟鞋| 亚洲系列中文字幕| 91av在线播放| 久久国产加勒比精品无码| 亚洲免费视频观看| 91豆花精品一区| 久久久999国产精品| 国产在线视频2019最新视频| 疯狂欧美牲乱大交777| 久久久久北条麻妃免费看| 1769国内精品视频在线播放| 欧美与欧洲交xxxx免费观看| 久久久久久999| 亚洲成人教育av| 日韩中文字幕网址| 亚洲人成在线观看| 亚洲第一中文字幕| 欧美又大又硬又粗bbbbb| 色狠狠av一区二区三区香蕉蜜桃| 亚洲成色777777在线观看影院| 亚洲电影成人av99爱色| 欧美性20hd另类| 久久久精品国产一区二区| 久久久亚洲欧洲日产国码aⅴ| 久久久中精品2020中文| 欧洲成人午夜免费大片| 亚洲日本中文字幕免费在线不卡| 亚洲国产欧美一区| 亚洲精品狠狠操| 国产男人精品视频| 超碰91人人草人人干| 亚洲国产成人久久综合| 成人a级免费视频| 亚洲一区999| 亚洲人高潮女人毛茸茸| 成人黄色网免费| 热久久这里只有| 欧美亚洲一区在线| 欧美日韩在线第一页| 日韩欧美中文字幕在线播放| 亚洲色图18p| 国产精品男人爽免费视频1| 国产99久久精品一区二区 夜夜躁日日躁| 精品少妇v888av| 九九热精品视频在线播放| 国产精品视频内| 亚洲色图色老头| 最新亚洲国产精品|