【问题标题】:How to get filesystem path to image cached with SDWebImage (iOS)如何获取使用 SDWebImage (iOS) 缓存的图像的文件系统路径
【发布时间】:2017-06-29 21:12:03
【问题描述】:

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

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

pass success block to the setImageWithURL method 的可能性但是,它得到的唯一参数是UIImage 实例(实际上在master 分支中还有一个名为cachedBOOL 变量正在也通过了)。那么 - 是否有可能直接从 UIImage 实例获取图像的文件系统路径?或者我应该修改 SDWebImage 以便将这些信息传递给缓存的实例?或者有没有更好的方法来实现我之前描述的目标?

我的 Show 类有 imageURL,下面是显示单元如何使用 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

【问题讨论】:

    标签: ios uiimage sdwebimage


    【解决方案1】:

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

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

    【讨论】:

    • 很好的答案!谢谢!我还需要使用下面的这两个函数来获取我的文件网址: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];
    • 不确定,但我记得,在现代版本的 SDWebImage 中,这些方法已经公开。只是没有检查这个。无论如何,很高兴它有帮助。
    • 关键是什么?完整的网址??
    • @Jonny 是的,图片的完整 http url。
    • 实际上当前版本的 SDImageCache 具有公共方法 cachePathForKey(),它为您提供了带有该键的图像文件的完整路径。
    【解决方案2】:

    我遇到了类似的问题,请参阅:SDWebImage showing placeholder for images in cache

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

    更新:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      相关资源
      最近更新 更多