如何获取使用SDWebImage(iOS)缓存的图像的文件系统路径

我在我的iOS应用程序中使用SDWebImage在UICollectionView上进行图像缓存。

一切都很好,但是,当用户在集合视图上快速滚动时,在使用缓存的图像替换占位符之前总是暂停一下。 我相信这是由于缓存检查。 为了获得更好的用户体验,我希望单元格在实际缓存图像后显示正确的图像而不是占位符。 如果我可以获得缓存图像的本地(设备)文件系统路径并将其存储在Show实例中,并使用它(如果存在)而不是我的占位符,这将很容易。

有可能将成功块传递给setImageWithURL方法但是,它获得的唯一参数是UIImage实例(实际上在master分支中还有一个名为cached的 BOOL变量也被传递)。 那么 – 是否有可能直接从UIImage实例获取图像的文件系统路径? 或者我应该修改SDWebImage,以便将该信息传递给缓存实例? 或者有没有更好的方法来实现我之前描述的目标?

我的Show类有imageURL ,这里是show cell使用SDWebImage的方法:

#import "ShowCell.h" #import  @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 

我遇到了类似的问题,请参阅: SDWebImage显示缓存中图像的占位符

基本上我分叉项目来解决这个问题。

更新:

您可以这样做以避免闪烁(这适用于项目的主分支):

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

有私有SDImageCache方法来获取缓存图像的文件系统路径。 我们可以将这些方法公之于众。 只需将下面的代码放入SDImageCache+Private.h然后将其添加到您的项目中:

 #import "SDImageCache.h" @interface SDImageCache (PrivateMethods) - (NSString *)defaultCachePathForKey:(NSString *)key; - (NSString *)cachedFileNameForKey:(NSString *)key; @end