iossdwebimage圖片緩存
1. ios開發 如何讓sdwebimage不緩存圖片 每次都重新載入url
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;
調用的時候設置下:
options = SDWebImageRefreshCached,該選項的含義如下:
/**
* Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
* The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
* This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
* If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
*
* Use this flag only if you can't make your URLs static with embeded cache busting parameter.
*/
2. ios開發中怎麼釋放imagenamed這個方法帶來的內存緩存
這個類庫提供一個UIImageView類別以支持載入來自網路的遠程圖片。具有緩存管理、非同步下載、同一個URL下載次數控制和優化等特徵。使用示範的代碼:UITableView使用UIImageView+WebCache類(基本應用,UIImageView的一個category)前提#import導入UIImageView+WebCache.h文件,然後在tableview的cellForRowAtIndexPath:方法下:-(UITableViewCell*)tableView:(UITableView*):(NSIndexPath*)indexPath{staticNSString*MyIdentifier=@"MyIdentifier";UITableViewCell*cell=[:MyIdentifier];if(cell==nil){cell=[[UITableViewCellalloc]initWithStyle::MyIdentifier]autorelease];}//:methodtoloadthewebimage[cell.imageViewsetImageWithURL:[NSURLURLWithString:@"/path/to/image.jpg"]placeholderImage:[UIImageimageNamed:@"placeholder.png"];cell.textLabel.text=@"MyText";returncell;}基本代碼:[imageViewsetImageWithURL:[NSURLURLWithString:@"/path/image.jpg"];使用SDWebImageManager類:可以進行一些非同步載入的工作。SDWebImageManager*manager=[];UIImage*cachedImage=[managerimageWithURL:url];//將需要緩存的圖片載入進來if(cachedImage){//如果Cache命中,則直接利用緩存的圖片進行有關操作//Usethecachedimageimmediatly}else{//如果Cache沒有命中,則去下載指定網路位置的圖片,並且給出一個委託方法//Startanasyncdownload[managerdownloadWithURL:urldelegate:self];}當然你的類要實現SDWebImageManagerDelegate協議,並且要實現協議的webImageManager:didFinishWithImage:方法。//當下載完成後,調用回調方法,使下載的圖片顯示-(void)webImageManager:(SDWebImageManager*):(UIImage*)image{//}獨立的非同步圖像下載可能會單獨用到非同步圖片下載,則一定要用downloaderWithURL:delegate:來建立一個SDWebImageDownloader實例。downloader=[:urldelegate:self];這樣SDWebImageDownloaderDelegate協議的方法imageDownloader:didFinishWithImage:被調用時下載會立即開始並完成。獨立的非同步圖像緩存SDImageCache類提供一個創建空緩存的實例,並用方法imageForKey:來尋找當前緩存。UIImage*myCachedImage=[SDImageCachesharedImageCache]imageFromKey:myCacheKey];存儲一個圖像到緩存是使用方法storeImage:forKey:[SDImageCachesharedImageCache]storeImage:myImageforKey:myCacheKey];默認情況下,圖像將被存儲在內存緩存和磁碟緩存中。如果僅僅是想內存緩存中,要使用storeImage:forKey:toDisk:方法的第三個參數帶一負值來替代。SDWebImage支持非同步的圖片下載+緩存,提供了UIImageView+WebCacha的category,方便使用。紀錄一下SDWebImage載入圖片的流程。入口setImageWithURL:placeholderImage:options:會先把placeholderImage顯示,然後SDWebImageManager根據URL開始處理圖片。進入SDWebImageManager-downloadWithURL:delegate:options:userInfo:,交給SDImageCache從緩存查找圖片是否已經下載queryDiskCacheForKey:delegate:userInfo:.先從內存圖片緩存查找是否有圖片,如果內存中已經有圖片緩存,SDImageCacheDelegate回調imageCache:didFindImage:forKey:userInfo:到SDWebImageManager。SDWebImageManagerDelegate回調webImageManager:didFinishWithImage:到UIImageView+WebCache等前端展示圖片。如果內存緩存中沒有,生成NSInvocationOperation添加到隊列開始從硬碟查找圖片是否已經緩存。根據URLKey在硬碟緩存目錄下嘗試讀取圖片文件。這一步是在NSOperation進行的操作,所以回主線程進行結果回調notifyDelegate:。如果上一操作從硬碟讀取到了圖片,將圖片添加到內存緩存中(如果空閑內存過小,會先清空內存緩存)。SDImageCacheDelegate回調imageCache:didFindImage:forKey:userInfo:。進而回調展示圖片。如果從硬碟緩存目錄讀取不到圖片,說明所有緩存都不存在該圖片,需要下載圖片,回調imageCache:didNotFindImageForKey:userInfo:。共享或重新生成一個下載器SDWebImageDownloader開始下載圖片。圖片下載由NSURLConnection來做,實現相關delegate來判斷圖片下載中、下載完成和下載失敗。connection:didReceiveData:中利用ImageIO做了按圖片下載進度載入效果。connectionDidFinishLoading:數據下載完成後交給SDWebImageDecoder做圖片解碼處理。圖片解碼處理在一個NSOperationQueue完成,不會拖慢主線程UI。如果有需要對下載的圖片進行二次處理,最好也在這里完成,效率會好很多。在主線程:宣告解碼完成,imageDecoder:didFinishDecodingImage:userInfo:回調給SDWebImageDownloader。imageDownloader:didFinishWithImage:回調給SDWebImageManager告知圖片下載完成。通知所有的downloadDelegates下載完成,回調給需要的地方展示圖片。將圖片保存到SDImageCache中,內存緩存和硬碟緩存同時保存。寫文件到硬碟也在以單獨NSInvocationOperation完成,避免拖慢主線程。SDImageCache在初始化的時候會注冊一些消息通知,在內存警告或退到後台的時候清理內存圖片緩存,應用結束的時候清理過期圖片。SDWI也提供了UIButton+WebCache和MKAnnotationView+WebCache,方便使用。SDWebImagePrefetcher可以預先下載圖片,方便後續使用。SDWebImage庫的作用:通過對UIImageView的類別擴展來實現非同步載入替換圖片的工作。
3. ios sdwebimage 方法怎麼棄用了
這個類庫提供一個UIImageView類別以支持載入來自網路的遠程圖片。具有緩存管理、非同步下載、同一個URL下載次數控制和優化等特徵。
使用示範的代碼:
UITableView使用UIImageView+WebCache類(基本應用,UIImageView的一個category)
前提#import導入UIImageView+WebCache.h文件,然後在tableview的cellForRowAtIndexPath:方法下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView :MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"];
cell.textLabel.text = @"My Text";
return cell;
}
基本代碼:[imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/image.jpg"];
使用SDWebImageManager類:可以進行一些非同步載入的工作。
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:url]; // 將需要緩存的圖片載入進來
if (cachedImage) {
// 如果Cache命中,則直接利用緩存的圖片進行有關操作
// Use the cached image immediatly
} else {
// 如果Cache沒有命中,則去下載指定網路位置的圖片,並且給出一個委託方法
// Start an async download
[manager downloadWithURL:url delegate:self];
}
當然你的類要實現SDWebImageManagerDelegate協議,並且要實現協議的webImageManager:didFinishWithImage:方法。
// 當下載完成後,調用回調方法,使下載的圖片顯示
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image {
// Do something with the downloaded image
}
獨立的非同步圖像下載
可能會單獨用到非同步圖片下載,則一定要用downloaderWithURL:delegate:來建立一個SDWebImageDownloader實例。
downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self];
這樣SDWebImageDownloaderDelegate協議的方法imageDownloader:didFinishWithImage:被調用時下載會立即開始並完成。
獨立的非同步圖像緩存
SDImageCache類提供一個創建空緩存的實例,並用方法imageForKey:來尋找當前緩存。
UIImage *myCachedImage = [SDImageCache sharedImageCache] imageFromKey:myCacheKey];
存儲一個圖像到緩存是使用方法storeImage: forKey:
[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
默認情況下,圖像將被存儲在內存緩存和磁碟緩存中。如果僅僅是想內存緩存中,要使用storeImage:forKey:toDisk:方法的第三個參數帶一負值
來替代。
SDWebImage 支持非同步的圖片下載+緩存,提供了 UIImageView+WebCacha 的 category,方便使用。紀錄一下 SDWebImage 載入圖片的流程。
入口 setImageWithURL:placeholderImage:options: 會先把 placeholderImage 顯示,然後 SDWebImageManager 根據 URL 開始處理圖片。
進入 SDWebImageManager-downloadWithURL:delegate:options:userInfo:,交給 SDImageCache 從緩存查找圖片是否已經下載 queryDiskCacheForKey:delegate:userInfo:.
先從內存圖片緩存查找是否有圖片,如果內存中已經有圖片緩存,SDImageCacheDelegate 回調 imageCache:didFindImage:forKey:userInfo: 到 SDWebImageManager。
SDWebImageManagerDelegate 回調 webImageManager:didFinishWithImage: 到 UIImageView+WebCache 等前端展示圖片。如果內存緩存中沒有,生成 NSInvocationOperation 添加到隊列開始從硬碟查找圖片是否已經緩存。
根據 URLKey 在硬碟緩存目錄下嘗試讀取圖片文件。這一步是在 NSOperation 進行的操作,所以回主線程進行結果回調 notifyDelegate:。
如果上一操作從硬碟讀取到了圖片,將圖片添加到內存緩存中(如果空閑內存過小,會先清空內存緩存)。SDImageCacheDelegate 回調 imageCache:didFindImage:forKey:userInfo:。進而回調展示圖片。
如果從硬碟緩存目錄讀取不到圖片,說明所有緩存都不存在該圖片,需要下載圖片,回調 imageCache:didNotFindImageForKey:userInfo:。
共享或重新生成一個下載器 SDWebImageDownloader 開始下載圖片。
圖片下載由 NSURLConnection 來做,實現相關 delegate 來判斷圖片下載中、下載完成和下載失敗。
connection:didReceiveData: 中利用 ImageIO 做了按圖片下載進度載入效果。
connectionDidFinishLoading: 數據下載完成後交給 SDWebImageDecoder 做圖片解碼處理。
圖片解碼處理在一個 NSOperationQueue 完成,不會拖慢主線程 UI。如果有需要對下載的圖片進行二次處理,最好也在這里完成,效率會好很多。
在主線程 : 宣告解碼完成,
imageDecoder:didFinishDecodingImage:userInfo: 回調給 SDWebImageDownloader。
imageDownloader:didFinishWithImage: 回調給 SDWebImageManager 告知圖片下載完成。
通知所有的 downloadDelegates 下載完成,回調給需要的地方展示圖片。
將圖片保存到 SDImageCache 中,內存緩存和硬碟緩存同時保存。寫文件到硬碟也在以單獨 NSInvocationOperation 完成,避免拖慢主線程。
SDImageCache 在初始化的時候會注冊一些消息通知,在內存警告或退到後台的時候清理內存圖片緩存,應用結束的時候清理過期圖片。
SDWI 也提供了 UIButton+WebCache 和 MKAnnotationView+WebCache,方便使用。
SDWebImagePrefetcher 可以預先下載圖片,方便後續使用。
SDWebImage庫的作用:
通過對UIImageView的類別擴展來實現非同步載入替換圖片的工作。
4. sdwebimage的內存緩存是怎麼實現的
就是要在適當的時候調用clearMemory方法。很顯然,這些緩存都放在這里memCache。
- (void)clearMemory {
[self.memCache removeAllObjects];
}
5. ios sdwebimage緩存是根據什麼
我使用SDWebImage的圖像在UICollectionView中我的 iOS 應用程序的緩存。
一切都很好,但是當用戶滾動快速的集合視圖有總是小暫停之前佔位符將被替換緩存的圖像。我相信這是因為緩存中檢查。為更好的用戶體驗,我想要顯示正確的圖像,而不是佔位符,一旦圖像實際上的高速緩存的單元格。如果我能得到的緩存的圖像 (設備) 本地文件系統路徑並將它存儲在該顯示的實例,並使用它,這將很容易 (如果存在) 而不是我的佔位符。
然而它獲取的唯一論據是UIImage實例 (實際上也有BOOL變數稱為緩存太正在傳遞的主分支) 中有向傳遞給setImageWithURL方法成功塊的可能性。所以-有沒有可能直接從UIImage實例獲取圖像的文件系統路徑?或我應該修改SDWebImage所以它傳遞的信息
如果您滿意,請點擊採納,我會非常開心的,謝謝您啦
6. ios sdwebimage 緩存在哪
可以查看SDImageCache的.m文件,
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
diskCacheDirectory:(nonnull NSString *)directory
查看這裡面寫的路徑
7. 用sdwebimage 來緩存圖片,怎樣清除指定url的緩存阿
清理緩存:
1、點擊打開一個IE。
2、點擊菜單欄中的"工具"菜單中的"internet選項"
3、在彈出的對話框中點擊"刪除文件".
4、在彈出的對話框中"刪除所有離線內容"打勾,之後 點確定.
5、點擊確定後,滑鼠可能會變成比較忙的狀態,這是因為緩存較多的緣故,一般情況下十秒左右滑鼠就會恢復正常。之後再點擊右下角的"確定"退出。這樣電腦IE的緩存就清除完畢了。
另外還可以用windos優化大師刪除
8. ios sdwebimage為什麼不調didfailwitherror協議方法
SDWebImage
這個類庫提供一個UIImageView
類別以支持載入來自網路的遠程圖片。具有緩存管理、非同步下載、同一個URL下載次數控制和優化等特徵。
SDWebImage 支持非同步的圖片下載+緩存,提供了 UIImageView+WebCacha 的
category,方便使用。SDWebImage載入圖片的流程:
1. 入口 setImageWithURL:placeholderImage:options: 會先把 placeholderImage
顯 示,然後 SDWebImageManager 根據 URL 開始處理圖片。
2. 進入 SDWebImageManager-downloadWithURL:delegate:options:userInfo:,交給
SDImageCache 從緩存查找圖片是否已經下載
queryDiskCacheForKey:delegate:userInfo:.
3.
先從內存圖片緩存查找是否有圖片,如果內存中已經有圖片緩存, SDImageCacheDelegate回調 imageCache:didFindImage:forKey:userInfo: 到
SDWebImageManager。
4. SDWebImageManagerDelegate 回調
webImageManager:didFinishWithImage: 到 UIImageView+WebCache等前端展示圖片。
5. 如果內存緩存中沒有,生成 NSInvocationOperation
添加到隊列開始從硬碟查找圖 片是否已經緩存。
6. 根據 URLKey在硬碟緩存目錄下嘗試讀取圖片文件。這一步是在 NSOperation
進 行的操作,所以回主線程進行結果回調 notifyDelegate:。
7. 如果上一操作從硬碟讀取到了圖片,將圖片添加到內存緩存中
(如果空閑內存過小, 會先清空內存緩存)。SDImageCacheDelegate回調
imageCache:didFindImage:forKey:userInfo:。進而回調展示圖片。
8. 如果從硬碟緩存目錄讀取不到圖片,說明所有緩存都不存在該圖片,
需要下載圖片, 回調 imageCache:didNotFindImageForKey:userInfo:。
9. 共享或重新生成一個下載器 SDWebImageDownloader 開始下載圖片。
10. 圖片下載由 NSURLConnection來做,實現相關 delegate
來判斷圖片下載中、下 載完成和下載失敗。
11. connection:didReceiveData: 中利用 ImageIO做了按圖片下載進度載入效果。
12. connectionDidFinishLoading: 數據下載完成後交給 SDWebImageDecoder
做圖 片解碼處理。
13. 圖片解碼處理在一個 NSOperationQueue完成,不會拖慢主線程 UI
。如果有需要 對下載的圖片進行二次處理,最好也在這里完成,效率會好很多。
14. 在主線程 :
宣告解碼完成, imageDecoder:didFinishDecodingImage:userInfo: 回調給
SDWebImageDownloader。
15. imageDownloader:didFinishWithImage: 回調給 SDWebImageManager
告知圖片 下載完成。
16. 通知所有的 downloadDelegates下載完成,回調給需要的地方展示圖片。
17. 將圖片保存到 SDImageCache
中,內存緩存和硬碟緩存同時保存。寫文件到硬碟 也在以單獨 NSInvocationOperation 完成,避免拖慢主線程。
18. SDImageCache 在初始化的時候會注冊一些消息通知,
在內存警告或退到後台的時 候清理內存圖片緩存,應用結束的時候清理過期圖片。
19. SDWI 也提供了 UIButton+WebCache 和 MKAnnotationView+WebCache
,方便 使用。
20. SDWebImagePrefetcher 可以預先下載圖片,方便後續使用
管理類的使用位置:
這個庫最常用到的,是UIImageView的一個Category:
UIImageView (WebCache)。
這裡面最常用的一個方法,就是根據URL,載入網路的圖片。它的實現如下:
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage*)placeholder
{
SDWebImageManager *manager = [];
// Remove in progress downloader from queue
[manager cancelForDelegate:self];
self.image = placeholder;
if (url)
{
[manager downloadWithURL:url delegate:self];
}
}
這個方法最大的好處就是,可以不需要改變UI的類,直接添加網路下載功能。
獨立的非同步圖像下載
可能會單獨用到非同步圖片下載,則一定要用downloaderWithURL:delegate:
來建立一個SDWebImageDownloader實例。
downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self];
這樣SDWebImageDownloaderDelegate
協議的方法 imageDownloader:didFinishWithImage:被調用時下載會立即開始並完成。
獨立的非同步圖像緩存
SDImageCache類提供一個創建空緩存的實例,並用方法imageForKey:來尋找當前緩存。
UIImage *myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey];
存儲一個圖像到緩存是使用方法storeImage: forKey:
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
默認情況下,圖像將被存儲在內存緩存和磁碟緩存中。如果僅僅是想內存緩存中,要使用storeImage:forKey:toDisk:方法的第三個參數帶一負值
來替代。
SDWebImage庫的作用:
通過對UIImageView的類別擴展來實現非同步載入替換圖片的工作。
主要用到的對象:
1、UIImageView (WebCache)類別,入口封裝,實現讀取圖片完成後的回調
2、SDWebImageManager,對圖片進行管理的中轉站,記錄那些圖片正在讀取。
向下層讀取Cache(調用SDImageCache
),或者向網路讀取對象(調用SDWebImageDownloader)。
實現SDImageCache和SDWebImageDownloader的回調。
3、SDImageCache,根據URL的MD5
摘要對圖片進行存儲和讀取(實現存在內存中或者存在硬碟上兩種實現)
實現圖片和內存清理工作。
4、SDWebImageDownloader,根據URL
向網路讀取數據(實現部分讀取和全部讀取後再通知回調兩種方式)
SDImageCache是怎麼做數據管理的?
SDImageCache分兩個部分,一個是內存層面的,一個是硬碟層面的。
內存層面的相當是個緩存器,以Key-Value
的形式存儲圖片。當內存不夠的時候會清除所有緩存圖片。
用搜索文件系統的方式做管理,文件替換方式是以時間為單位,
剔除時間大於一周的圖片文件。
當SDWebImageManager向SDImageCache
要資源時,先搜索內存層面的數據,如果有直接返回,沒有的話去訪問磁碟,將圖片從磁碟讀取出來,然後做Decoder
,將圖片對象放到內存層面做備份,再返回調用層
9. ios sdwebimage怎麼判斷清除緩存完成
1.找到SDImageCache類
2.添加如下方法:
- (float)checkTmpSize
{
float totalSize = 0;
NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:diskCachePath];
for (NSString *fileName in fileEnumerator)
{
NSString *filePath = [diskCachePath :fileName];
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
unsigned long long length = [attrs fileSize];
totalSize += length / 1024.0 / 1024.0;
}
// NSLog(@"tmp size is %.2f",totalSize);
return totalSize;
}
新版的SDImageCache類,已增加此方法
[objc] view plain
[[SDImageCache sharedImageCache] getSize];
3.在設置里這樣使用
[objc] view plain
#pragma 清理緩存圖片
- (void)clearTmpPics
{
[[SDImageCache sharedImageCache] clearDisk];
// [[SDImageCache sharedImageCache] clearMemory];//可有可無
DLog(@"clear disk");
float tmpSize = [[SDImageCache sharedImageCache] checkTmpSize];
NSString *clearCacheName = tmpSize >= 1 ? [NSString stringWithFormat:@"清理緩存(%.2fM)",tmpSize] : [NSString stringWithFormat:@"清理緩存(%.2fK)",tmpSize * 1024];
[configDataArray replaceObjectAtIndex:2 withObject:clearCacheName];
[configTableView reloadData];
}