Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

uiimage - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...