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

问题描述:

我正在 SDWebImage 进行图片缓存我的iOS应用程序中的.apple.com / library / ios /#documentation / UIKit / Reference / UICollectionView_class / Reference / Reference.htmlrel =nofollow noreferrer> UICollectionView 。

I'm using SDWebImage for image caching at UICollectionView in my iOS app.

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

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.

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

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?

我的显示类有 imageURL ,这里是如何显示单元格使用SDWebImage:

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


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

I had a similar problem, see: SDWebImage showing placeholder for images in cache

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

Basically I forked the project to resolve this.

更新:

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

You can just do this to avoid the flickering (this works with the master branch of the project):

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