SDWebImage: High memory use

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:post.imageURL options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) 
{
    // float progress = (float)receivedSize/expectedSize;
    // [cell.imageViewFoto updateProgress:progress];
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
    if (image) {
        [cell.imageViewFoto setupImageView:post indexPath:indexPath isGrid:NO  image:image];
    }
}];
captura de tela 2016-04-30 as 15 34 24

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 38 (16 by maintainers)

Most upvoted comments

After lots of R&D and applying all kind of options for SDImageCache, I have finalized below options to set to avoid crash on heavy images load with the Best performance on RAM cache load:

Hope this will help you all !!!

Add below code into your AppDelegate class:

import SDWebImage

SDImageCache.shared().config.maxCacheAge = 3600 * 24 * 7 //1 Week
        
SDImageCache.shared().maxMemoryCost = 1024 * 1024 * 20 //Aprox 20 images
        
//SDImageCache.shared().config.shouldCacheImagesInMemory = false //Default True => Store images in RAM cache for Fast performance
        
SDImageCache.shared().config.shouldDecompressImages = false
        
SDWebImageDownloader.shared().shouldDecompressImages = false
        
SDImageCache.shared().config.diskCacheReadingOptions = NSData.ReadingOptions.mappedIfSafe

How to use:

//MARK:- Load Image with Activity Indicator

    func loadImageWith(imgView: UIImageView, url: String?) {
        
        imgView.sd_setShowActivityIndicatorView(true)
        imgView.sd_setIndicatorStyle(UIActivityIndicatorViewStyle.gray)
        if url != nil {
            imgView.sd_setImage(with: URL.init(string: url!), placeholderImage: UIImage.init(named: "ic_placeholder"), options:SDWebImageOptions.scaleDownLargeImages, completed: { (image: UIImage?, error: Error?, cacheType: SDImageCacheType, url: URL?) in
                
                if ((error) != nil) {
                    imgView.image = UIImage.init(named: "ic_placeholder")
                }
            })
        }
        else{
            imgView.image = UIImage.init(named: "ic_placeholder")
        }
    }

loadImageWith(imgView: cell.imgViewProduct, url: product.p_image)