2013-02-15 61 views
17

你好我正在項目中使用SDWebImage框架,我想下載和緩存圖像,但我認爲我的代碼是在緩存中存儲兩次圖像?有沒有辦法通過一個鍵將圖像存儲在緩存中一次?這是我的代碼。SDWebImage下載圖像並存儲到緩存的密鑰

  SDWebImageManager *manager = [SDWebImageManager sharedManager]; 
     [manager downloadWithURL:[NSURL URLWithString:url] options:0 progress:^(NSUInteger receivedSize, long long expectedSize) { 

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

      if(image){ 
       NSString *localKey = [NSString stringWithFormat:@"Item-%d", i]; 
       [[SDImageCache sharedImageCache] storeImage:image forKey:localKey]; 
      } 

      }]; 

有什麼我錯過了嗎?看起來在我的分配工具中這樣做會佔用大量內存。

回答

23

我很驚訝沒有人回答這個問題,但我有類似的問題,並且遇到了這個問題,所以我會回答這個問題,讓人們看到這個前進(假設你現在已經對此進行了整理) 。

要直接回答你的問題,是的,你正在緩存圖像兩次。

將呼叫下載到SDWebImageManager會根據圖像的url的絕對字符串自動緩存圖像。如果你想要自己的密鑰,你可以在SDWebImageDownloader上使用下載調用,據我所知,默認情況下不會緩存。從那裏你可以像你已經在做的那樣調用sharedImageCache並且緩存你想要的任何密鑰。除此之外,很奇怪,無論如何,由於SDWebImage喜歡緩存到磁盤而不是一般的內存,所以很難看出分配堆積如山。也許其他事情正在同時發生?

希望這有助於

布蘭登

+0

你的意思是,如果我們爲每一個(固定下載圖像URL)鍵,它會不會開除下載事件在網絡上?對 ? 它會使用先前的緩存嗎? – 2013-11-28 07:09:42

+0

自從我看了這個以後,過了一段時間,但如果您計劃使用特定的緩存鍵存儲圖像,則在進行下載請求之前,應檢查圖像的[[SDImageCache sharedImageCache]]。如果您沒有它,請使用您的特定密鑰進行存儲,以便下次使用。現在這可能已經改變了,但是如果你要這樣做,你應該使用'SDWebImageDownloader',然後存儲你自己的密鑰。 – Stakenborg 2013-12-02 14:50:39

-1
SDWebImageManager *manager = [SDWebImageManager sharedManager]; 

[manager downloadImageWithURL:ImageUrl options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) 

{ 


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

    if(image){ 


     NSLog(@"image=====%@",image); 
    } 
}]; 
4

SDWebImage將圖像緩存到磁盤以及內存。讓我們通過這個:

  1. 你從一個新的網址下載圖片。
  2. 它被緩存到內存和磁盤。
  3. 如果您在同一會話中調用圖像,則會從內存中檢索該圖像。
  4. 讓我們假設你重新運行應用程序,然後訪問url,它會檢查內存,圖像不在那裏,然後它會檢查磁盤,並從那裏得到它。如果沒有,它會下載它。
  5. 圖像在標準設置下在磁盤上保留一週。

所以,你不需要擔心緩存。 SDWebImage照顧它非常糟糕。

如果您希望根據HTTP緩存頭設置設置,則可以執行緩存的自定義實現以及緩存中的圖像刷新。

你可以在他們的github頁面上找到完整的細節here

12

夫特使用下面的代碼下載的圖像,並且將其存儲在高速緩存中:

//SDWebImageManager download image with High Priority 

    SDWebImageManager.sharedManager().downloadImageWithURL(NSURL(string: imageUrl), options: SDWebImageOptions.HighPriority, progress: { (receivedSize :Int, ExpectedSize :Int) in 
      SVProgressHUD.show() 
      }, completed: { (image :UIImage!, error:NSError!, cacheType :SDImageCacheType, finished :Bool,imageUrl: NSURL!) in 
       if(finished) { 
        SVProgressHUD.dismiss() 
        if((image) != nil) { 
         //image downloaded do your stuff 
        } 
       } 
     }) 

夫特3版本:

SDWebImageManager.shared().downloadImage(with: NSURL(string: "...") as URL!, options: .continueInBackground, progress: { 
(receivedSize :Int, ExpectedSize :Int) in 

}, completed: { 
(image : UIImage?, error : Error?, cacheType : SDImageCacheType, finished : Bool, url : URL?) in 

}) 

目標C版本:

[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { 

        if (image && finished) { 
         // Cache image to disk or memory 
         [[SDImageCache sharedImageCache] storeImage:image forKey:CUSTOM_KEY toDisk:YES];  
        } 
       }]; 
+0

謝謝你parth。我還沒有看過Swift版本,但Swift版本是否有SDWebImageDownloader類實現來用自定義密鑰而不是URL來緩存圖像? – AgnosticDev 2016-11-24 20:40:52

+0

是的swift版本有緩存你的形象custome密鑰的類。 您可以使用以下代碼, SDImageCache.sharedImageCache()。storeImage(image,forKey:「\(file.attachmentFileName!)_ \(file.fileId)」) – parth 2016-11-25 09:07:52

+0

太棒了!最後使用自定義密鑰緩存圖像。 – yuchen 2017-01-19 03:23:51

0

//創建的是定製的ImageView,並通過圖片URL由array(INDEXPATH.ROW)

(void) loadImage:(NSString *)imageLink{ 

    imageLink = [imageLink stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 

    SDWebImageManager *manager = [SDWebImageManager sharedManager]; 

    [manager loadImageWithURL:[NSURL URLWithString:imageLink] options:SDWebImageDelayPlaceholder progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) { 

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

     imageFrame.image = image; 

    }]; 
}