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

标签 ios uiimage sdwebimage

我正在使用 SDWebImage用于图像缓存 UICollectionView在我的 iOS 应用中。

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

有可能pass success block to the setImageWithURL method但是,它获得的唯一参数是 UIImage实例(实际上在 master 分支中也传递了名为 cachedBOOL 变量)。那么 - 是否有可能直接从 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/

相关文章:

ios - 视频轨道后面的 AVFoundation UIImage

swift - iOS swift : SDWebImage Image Loading Error

ios - 使用 SDWebImage 下载的图像的 UIImageView 幻灯片

ios - 从 iOS 应用程序中的 S3 存储桶中删除文件 (Swift)

iphone - 使用自定义部分高度时,UITableView 在弹跳后不会返回到正确的位置

iphone - 在 iOS 中使用 UIWebView 来渲染图像而不是 UIImageView 使用的内存要少得多。说得通?

ios - MKMapSnapshotter 有效,但我无法共享 UIImage

ios - 使用没有文件扩展名的 SDWebImage 加载图像

ios - 尝试使用 swiftJson 解析 json 数组

objective-c - 在 UITableView 数据源中使用 "commitEditingStyle"时显示另一个按钮