2013-02-15 75 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; 

    }]; 
}