SDWebImage Download image and store to cache for key

后端 未结 11 1485
失恋的感觉
失恋的感觉 2020-12-24 03:30

Hello I am using the SDWebImage framework in a project and I want to download and cache images, but I think my code is storing an image in the cache twice? Is there any way

相关标签:
11条回答
  • 2020-12-24 03:58

    Swift 4:

    SDWebImageManager.shared().loadImage(
            with: URL(string: imageUrl),
            options: .highPriority,
            progress: nil) { (image, data, error, cacheType, isFinished, imageUrl) in
              print(isFinished)
          }
    
    0 讨论(0)
  • 2020-12-24 04:00
    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);
        }
    }];
    
    0 讨论(0)
  • 2020-12-24 04:02

    Yes is possible to download the image using SDWebImage And store into local memory Manually.

    Downloaded Image store into local memory using SDWebImage

    func saveImage(url: URL, toCache: UIImage?, complation: @escaping SDWebImageNoParamsBlock) {
        guard let toCache = toCache else { return }
    
        let manager = SDWebImageManager.shared()
        if let key = manager.cacheKey(for: url) {
            manager.imageCache?.store(toCache, forKey: key, completion: complation)
        }
    }
    

    Load image from memory using image URL

    static func imageFromMemory(for url: String) -> UIImage? {
        if let encoded = url.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
            let url = URL(string: encoded) {
            let manager = SDWebImageManager.shared()
            if let key: String = manager.cacheKey(for: url),
                let image = manager.imageCache?.imageFromMemoryCache(forKey: key) {
                return image
            }
        }
        return nil
    }
    
    0 讨论(0)
  • 2020-12-24 04:04

    SDWebImage caches the image both to disk as well as memory. Let's go through this:

    1. You download the image from a new url.
    2. It gets cached to memory and disk.
    3. If you call the image in the same session, it is retrieved from the memory.
    4. Let's say you re-run the app and then access the url, it will check the memory, where the image won't be there, then it will check the disk, and get it from there. If not, it will download it.
    5. The image stays in disk for a week by the standard setting.

    So, you don't need to worry about caching. SDWebImage takes care of it pretty damn well.

    You can do custom implementation for caching as well as image refresh from the cache in case you want the settings as per your HTTP caching header as well.

    You can find the complete details on their github page here.

    0 讨论(0)
  • 2020-12-24 04:07

    In Swift use the code below to download an image and to store it in the cache:

    //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
                        }
                    }
            })
    

    Swift 3 version:

    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
    
    })
    

    Objective-C version:

    [[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 讨论(0)
提交回复
热议问题