3

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
Keyur
  • 180
  • 1
  • 5
  • 20
lukaszb
  • 694
  • 1
  • 6
  • 15

2 Answers2

6

There are private SDImageCache methods to get filesystem path to cached image. We can make these methods public with a category. Just put the code below into the, say, SDImageCache+Private.h and add it to your project:

#import "SDImageCache.h"

@interface SDImageCache (PrivateMethods)

- (NSString *)defaultCachePathForKey:(NSString *)key;
- (NSString *)cachedFileNameForKey:(NSString *)key;

@end
mkll
  • 2,058
  • 1
  • 15
  • 21
  • Great Answer! thanks! I also needed to turn use those two functions like below to get my file url: `SDImageCache *imageCache = [SDImageCache sharedImageCache]; NSArray *arrayOfPathComponents = [NSArray arrayWithObjects: [imageCache defaultCachePathForKey:imageUrlString],[imageCache cachedFileNameForKey:imageUrlString],nil]; NSString* path = [NSString pathWithComponents: arrayOfPathComponents]; NSURL *urlOfTheLocalFile = [NSURL fileURLWithPath: path];` – drpawelo May 28 '15 at 16:06
  • Not sure, but, as I remember, in modern versions of SDWebImage these methods are already public. Just did not check this out. Anyway, glad it helps. – mkll May 29 '15 at 04:04
  • What is the key? The full url?? – Jonny Apr 19 '17 at 02:51
  • 1
    @Jonny Yes, the full http url of the image. – mkll Apr 19 '17 at 06:24
  • 2
    Actually the current version of **SDImageCache** has public method *cachePathForKey()* which gives you full path to the image file with that key. – Borzh Jul 13 '21 at 14:20
0

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];
Community
  • 1
  • 1
rufo
  • 5,158
  • 2
  • 36
  • 47