Search code examples
iosuiimagesdwebimage

How to get filesystem path to image cached with SDWebImage (iOS)


I'm using SDWebImage for image caching at UICollectionView in my iOS app.

Everything is fine, however, when user scrolls fast over the collection view there is always little pause before placeholder is replaced with the cached image. I believe this is due to cache checks. For better user experience I would like cells to show the proper image instead of placeholder once an image is actually cached. This would be easy if I could get local (device) filesystem's path of the cached image and store it at the Show instance, and use it (if exists) instead of my placeholder.

There is the possibility to pass success block to the setImageWithURL method However, the only argument it gets is UIImage instance (actually in master branch there is also BOOL variable called cached being passed too). So - is there a possibility to get image's filesystem path straight from the UIImage instance? Or should I modify SDWebImage so it pass that information along to cached instance? Or is there any better way achieve a goal I described earlier?

My Show class has imageURL and here is how show cell use SDWebImage:

#import "ShowCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

@implementation ShowCell

@synthesize imageView = _imageView;
@synthesize label = _label;
@synthesize show = _show;

- (void)setShow:(Show *)show
{
    if (_show != show) {
        self.label.text = show.title;
        if (show.imageURL) {
            [self.imageView setImageWithURL:[NSURL URLWithString:show.imageURL]
                           placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        }
        _show = show;
    }
}

@end

Solution

  • I had a similar problem, see: SDWebImage showing placeholder for images in cache

    Basically I forked the project to resolve this.

    Update:

    You can just do this to avoid the flickering (this works with the master branch of the project):

    [imageView setImageWithURL:url placeholderImage:imageView.image options:0 
     progress:^(NSUInteger receivedSize, long long expectedSize) {
            imageView.image = [UIImage imageNamed:@"QuestionMarkFace.png"];
     } completed:nil];