SDWebImage下载图像并存储到密钥缓存

Agn*_*Dev 23 objective-c uiimage 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];
            }

          }];  
Run Code Online (Sandbox Code Playgroud)

有什么我错过了吗?看起来在我的分配工具中这样做会增加大量内存.

Sta*_*org 27

我很惊讶没有人回答这个问题,但是我遇到了类似的问题并且遇到了这个问题,所以我会回答这个问题,因为有人在看这个问题(假设你现在已经把它排除了).

要直接回答您的问题,是的,您要将图像缓存两次.

下载对SDWebImageManager的调用会根据图像url的absoluteString自动使用键缓存图像.如果你想要自己的密钥,可以在SDWebImageDownloader上使用下载调用,据我所知,默认情况下不会缓存.从那里你可以像你正在做的那样调用sharedImageCache并使用你想要的任何键进行缓存.

除此之外,在任何情况下,由于SDWebImage喜欢缓存到磁盘而不是内存,因此看到分配堆积很奇怪.也许还有其他事情在同一时间发生?

希望这可以帮助,

布兰登


小智 19

Swift中使用下面的代码下载图像并将其存储在缓存中:

//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
                    }
                }
        })
Run Code Online (Sandbox Code Playgroud)

Swift 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

})
Run Code Online (Sandbox Code Playgroud)

Objective-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];     
                    }
                }]; 
Run Code Online (Sandbox Code Playgroud)


Doe*_*ata 10

SWIFT 5 和最新的 SDWebImage 5.2.3

SDWebImageManager.shared.loadImage(
    with: album.artUrlFor(imageShape: .square),
    options: .continueInBackground, // or .highPriority
    progress: nil,
    completed: { [weak self] (image, data, error, cacheType, finished, url) in
        guard let sself = self else { return }

        if let err = error {
            // Do something with the error
            return
        }

        guard let img = image else {
            // No image handle this error
            return
        }

        // Do something with image
    }
)
Run Code Online (Sandbox Code Playgroud)


Gau*_*ain 5

SDWebImage 将图像缓存到磁盘和内存中。让我们来看看这个:

  1. 您从新网址下载图像。
  2. 它被缓存到内存和磁盘。
  3. 如果您在同一会话中调用图像,则会从内存中检索它。
  4. 假设您重新运行应用程序,然后访问 url,它将检查内存,图像将不在那里,然后它会检查磁盘,并从那里获取它。如果没有,它会下载它。
  5. 图像按照标准设置在磁盘中保留一周。

因此,您无需担心缓存。SDWebImage 处理得非常好。

如果您还希望根据 HTTP 缓存标头进行设置,您可以为缓存进行自定义实现以及从缓存中刷新图像。

您可以在此处的 github 页面上找到完整的详细信息。


rez*_*afi 5

试试Swift 4.0

let url = URL(string: imageUrl!)

SDWebImageManager.shared().imageDownloader?.downloadImage(with: url, options: .continueInBackground, progress: nil, completed: {(image:UIImage?, data:Data?, error:Error?, finished:Bool) in
     if image != nil {. 

     }
})
Run Code Online (Sandbox Code Playgroud)


Bog*_*kiy 5

斯威夫特4:

SDWebImageManager.shared().loadImage(
        with: URL(string: imageUrl),
        options: .highPriority,
        progress: nil) { (image, data, error, cacheType, isFinished, imageUrl) in
          print(isFinished)
      }
Run Code Online (Sandbox Code Playgroud)