SDWebImage Download image and store to cache for key

后端 未结 11 1484
失恋的感觉
失恋的感觉 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:46

    Try APSmartStorage (https://github.com/Alterplay/APSmartStorage) instead of SDWebImage.

    APSmartStorage gets data from network and automatically caches data on disk or in memory in a smart configurable way. Should be good enough for your task.

    0 讨论(0)
  • 2020-12-24 03:48

    //CREATE A CUSTOME IMAGEVIEW AND PASS THE IMAGE URL BY 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;
    
        }];
    }
    
    0 讨论(0)
  • 2020-12-24 03:51

    Swift 5.1

    import UIKit
    import SDWebImage
    
    extension UIImageView{
        func downloadImage(url:String){
          //remove space if a url contains.
            let stringWithoutWhitespace = url.replacingOccurrences(of: " ", with: "%20", options: .regularExpression)
            self.sd_imageIndicator = SDWebImageActivityIndicator.gray
            self.sd_setImage(with: URL(string: stringWithoutWhitespace), placeholderImage: UIImage())
        }
    }
    

    How to use

    let myUrl = "https://www.example.com"
    myImageView.downloadImage(url: myUrl)
    
    
    0 讨论(0)
  • 2020-12-24 03:52

    Try this for 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 {. 
    
         }
    })
    
    0 讨论(0)
  • 2020-12-24 03:55

    I'm surprised nobody answered this question, but I've had a similar question and came across this, so I'll answer it for people viewing this going forward (assuming you've sorted this out yourself by now).

    To directly answer your question, yes, you are caching the image twice.

    Download calls to SDWebImageManager automatically cache images with keys based on the absoluteString of the image's url. If you want your own key, you can use the download call on SDWebImageDownloader which as far as I can tell does NOT cache by default. From there you can call the sharedImageCache as you're already doing and cache with whatever key you want.

    That aside, it is strange you're seeing allocations piling up in any case as SDWebImage likes to cache to disk and not memory generally. Maybe something else is going on at the same time?

    Hope this helps,

    -Brandon

    0 讨论(0)
  • 2020-12-24 03:55

    SWIFT 5 & Latest 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
        }
    )
    
    0 讨论(0)
提交回复
热议问题