ios - 具有永久缓存的 sdwebimage
<p><p>我正在开发一个 iOS 应用程序,它需要 SDWebImage 将图片永久缓存到手机上。 </p>
<ol>
<li>SDWebImage代码中有一个过期设置,我是否应该将过期时间设置为一个较大的值以永久存储缓存? </li>
<li>既然我想永久缓存图片,我应该将它们存储在专用文件夹中还是默认目录就足够了?我的应用程序需要这些图片在应用程序关闭和重新打开以及手机重新启动时保持不变。 </li>
<li>如果我想永久缓存图片,除了将到期时间设置为一个较大的值外,还有什么我需要注意的吗? </li>
</ol>
<p>谢谢。 </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>遗憾的是 SDWebImage 没有提供这样的能力
所以为了利用 SDWebImage 提供的高级缓存功能,我围绕它编写了一个包装器</p>
<p>基本上这个类管理一个后备永久缓存,所以如果在 SDWeb 图像缓存“磁盘和内存”中找不到请求的图像,它将在永久缓存中寻找它
如果找到永久缓存,它将被复制到 SDwebImage 缓存中,以便在以后的请求中使用其内存 </p>
<p>使用这种方法,我设法像往常一样使用 SDWebImage 将图像设置为表格单元格
您还可以在需要时触发 clearImageCache 来清除永久缓存</p>
<p><strong>.h文件</strong></p>
<pre><code>@interface CacheManager : NSObject
+ (CacheManager*)sharedManager;
// Images
- (BOOL) diskImageExistsForURL:(NSURL*) url;
- (void) downloadImage:(NSURL*) url completed:(void(^)(UIImage*))onComplete;
- (void) setImage:(NSURL*)url toImageView:(UIImageView*)iv completed:(void(^)(UIImage*))onComplete;
- (void) clearImageCache;
@end
</code></pre>
<p><strong>.m 文件</strong></p>
<pre><code>#import "CacheManager.h"
#import <SDWebImage/UIImageView+WebCache.h>
#define CACH_IMAGES_FOLDER @"ImagesData"
@implementation CacheManager
static CacheManager *sharedManager = nil;
#pragma mark -
#pragma mark Singilton Init Methods
// init shared Cache singleton.
+ (CacheManager*)sharedManager{
@synchronized(self){
if ( !sharedManager ){
sharedManager = [ init];
}
}
return sharedManager;
}
// Dealloc shared API singleton.
+ (id)alloc{
@synchronized( self ){
NSAssert(sharedManager == nil, @"Attempted to allocate a second instance of a singleton.");
return ;
}
return nil;
}
// Init the manager
- (id)init{
if ( self = ){}
return self;
}
/**
@returns YES if image found in the permanent cache or the cache managed by SDWebImage lib
*/
- (BOOL) diskImageExistsForURL:(NSURL*) url{
// look for image in the SDWebImage cache
SDWebImageManager *manager = ;
if()
return YES;
// look for image in the permanent cache
NSString *stringPath = url.path;
NSFileManager *fileManager = ;
return ;
}
/**
get the image with specified remote url asynchronosly
first looks for the image in SDWeb cache to make use of the disk and memory cache provided by SDWebImage
if not found, looks for it in the permanent cache we are managing, finally if not found in either places
it will download it using SDWebImage and cache it.
*/
- (void) downloadImage:(NSURL*) url completed:(void(^)(UIImage*))onComplete{
NSString *localPath = [ path];
NSFileManager *fileManager = ;
// -1 look for image in SDWeb cache
SDWebImageManager *manager = ;
if(){
[manager downloadImageWithURL:url options:SDWebImageRetryFailed
progress:^(NSInteger receivedSize, NSInteger expectedSize) {}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
onComplete(image);
// save the image to the perminant cache for later
// if not saved before
if(image){
if (){
NSURL* localeUrl = ;
;
}
}
}];
return;
}
// -2 look for the image in the permanent cache
if (){
UIImage *img = ;
onComplete(img);
// save image back to the SDWeb image cache to make use of the memory cache
// provided by SDWebImage in later requests
;
return;
}
// -3 download the image using SDWebImage lib
[manager downloadImageWithURL:url options:SDWebImageRetryFailed
progress:^(NSInteger receivedSize, NSInteger expectedSize) {}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
onComplete(image);
// save the image to the permanent cache for later
if(image){
NSURL* localeUrl = ;
;
}
}];
}
- (void) setImage:(NSURL*)url toImageView:(UIImageView*)iv completed:(void(^)(UIImage*))onComplete{
[self downloadImage:url completed:^(UIImage * downloadedImage) {
iv.image = downloadedImage;
onComplete(downloadedImage);
}];
}
/**
@param:imgUrl : local url of image to read
*/
- (UIImage*) getImageFromCache:(NSURL*)imgUrl{
return ];
}
/**
writes the suplied image to the local path provided
*/
-(void) saveImage:(UIImage*)img toCacheWithLocalPath:(NSURL*)localPath{
NSData * binaryImageData = UIImagePNGRepresentation(img);
atomically:YES];
}
// Generate local image URL baesd on the name of the remote image
// this assumes the remote images already has unique names
- (NSURL*)getLocalUrlForImageUrl:(NSURL*)imgUrl{
// Saving an offline copy of the data.
NSFileManager *fileManager = ;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = ;
NSString *folderPath = ;
BOOL isDir;
// create folder not exist
if (!){
NSError *dirWriteError = nil;
if (!){
NSLog(@"Error: failed to create folder!");
}
}
NSString *imgName = [[ lastPathComponent] stringByDeletingPathExtension];
NSURL *cachesDirectoryURL = [ URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSString *pathString = ;
return ;
}
/**
removes the folder contating the cahced images,
the folder will be reacreated whenever a new image is being saved to the permanent cache
*/
-(void)clearImageCache{
// set the directory path
NSFileManager *fileManager = ;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = ;
NSString *folderPath =;
BOOL isDir;
NSError *dirError = nil;
// folder exist
if (){
if (!)
NSLog(@"Failed to remove folder");
}
}
@end
</code></pre></p>
<p style="font-size: 20px;">关于ios - 具有永久缓存的 sdwebimage,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39642465/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39642465/
</a>
</p>
页:
[1]