我正在使用 SDWebImage用于图像缓存 UICollectionView在我的 iOS 应用中。
一切都很好,但是,当用户在 Collection View 上快速滚动时,在占位符被缓存的图像替换之前总是有很少的停顿。我相信这是由于缓存检查。为了更好的用户体验,我希望单元格在实际缓存图像后显示正确的图像而不是占位符。如果我可以获得缓存图像的本地(设备)文件系统路径并将其存储在 Show 实例中,并使用它(如果存在)而不是我的占位符,这将很容易。
有可能pass success block to the setImageWithURL method但是,它获得的唯一参数是 UIImage实例(实际上在 master 分支中也传递了名为 cached 的 BOOL 变量)。那么 - 是否有可能直接从 UIImage 实例获取图像的文件系统路径?或者我应该修改 SDWebImage 以便将该信息传递给缓存的实例吗?或者有没有更好的方法来实现我之前描述的目标?
我的 Show 类有 imageURL,这里是 show cell 如何使用 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
最佳答案
有私有(private)的 SDImageCache
方法来获取缓存图像的文件系统路径。我们可以通过类别公开这些方法。只需将下面的代码放入 SDImageCache+Private.h
并将其添加到您的项目中:
#import "SDImageCache.h"
@interface SDImageCache (PrivateMethods)
- (NSString *)defaultCachePathForKey:(NSString *)key;
- (NSString *)cachedFileNameForKey:(NSString *)key;
@end
关于ios - 如何获取使用 SDWebImage (iOS) 缓存的图像的文件系统路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12768264/