ios - sdwebimage with permanent caching -


i developing ios app requires sdwebimage permanently cache pictures onto phone.

  1. there expiration setting in sdwebimage codes, should set expiration time large value store cache permanently?
  2. since wanted pictures cached permanently, should stored them dedicated folder or default directory sufficient? app needs picture persistent when app closed , re-open when phone restarted.
  3. if wanted cache picture permanently, there else need watch out other setting expiration large value?

thanks.

unfortunately sdwebimage not provide such capability make use of advanced caching capabilities provided sdwebimage wrote wrapper around it

basically class manages fallback permanent cache if requested image not found in sdweb image cache "disk , memory" in permanent cache , if found permanent cache copied sdwebimage cache make use of memory cashing in later requests

using approach managed keep setting images table cells smooth usual sdwebimage can trigger clearimagecache errase permanent cache whenever need

.h file

@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 

.m file

#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 = [[cachemanager alloc] init];         }     }     return sharedmanager; }  // dealloc shared api singleton. + (id)alloc{     @synchronized( self ){         nsassert(sharedmanager == nil, @"attempted allocate second instance of singleton.");         return [super alloc];     }     return nil; }  // init manager - (id)init{     if ( self = [super init] ){}     return self; }  /**   @returns yes if image found in permanent cache or cache managed sdwebimage lib  */ - (bool) diskimageexistsforurl:(nsurl*) url{      // image in sdwebimage cache     sdwebimagemanager *manager = [sdwebimagemanager sharedmanager];     if([manager diskimageexistsforurl:url])         return yes;      // image in permanent cache     nsstring *stringpath = url.path;     nsfilemanager *filemanager = [nsfilemanager defaultmanager];     return [filemanager fileexistsatpath:stringpath]; }  /**  image specified remote url asynchronosly   first looks image in sdweb cache make use of disk , memory cache provided sdwebimage  if not found, looks in permanent cache managing, if not found in either places  download using sdwebimage , cache it.  */ - (void) downloadimage:(nsurl*) url completed:(void(^)(uiimage*))oncomplete{      nsstring *localpath = [[self getlocalurlforimageurl:url] path];     nsfilemanager *filemanager = [nsfilemanager defaultmanager];      // -1 image in sdweb cache     sdwebimagemanager *manager = [sdwebimagemanager sharedmanager];     if([manager diskimageexistsforurl:url]){         [manager downloadimagewithurl:url options:sdwebimageretryfailed                              progress:^(nsinteger receivedsize, nsinteger expectedsize) {}                             completed:^(uiimage *image, nserror *error, sdimagecachetype cachetype, bool finished, nsurl *imageurl) {                                 oncomplete(image);                                  // save image perminant cache later                                 // if not saved before                                 if(image){                                     if ([filemanager fileexistsatpath:localpath]){                                         nsurl* localeurl = [self getlocalurlforimageurl:url];                                         [self saveimage:image tocachewithlocalpath:localeurl];                                     }                                 }                             }];         return;     }      // -2 image in permanent cache     if ([filemanager fileexistsatpath:localpath]){         uiimage *img = [self getimagefromcache:url];         oncomplete(img);         // save image sdweb image cache make use of memory cache         // provided sdwebimage in later requests         [manager saveimagetocache:img forurl:url];         return;     }      // -3 download 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 image permanent cache later                             if(image){                                 nsurl* localeurl = [self getlocalurlforimageurl:url];                                 [self saveimage:image tocachewithlocalpath: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 read  */ - (uiimage*) getimagefromcache:(nsurl*)imgurl{     return [uiimage imagewithdata: [nsdata datawithcontentsofurl:imgurl]]; }  /**  writes suplied image local path provided  */ -(void) saveimage:(uiimage*)img tocachewithlocalpath:(nsurl*)localpath{     nsdata * binaryimagedata = uiimagepngrepresentation(img);     [binaryimagedata writetofile:[localpath path] atomically:yes]; }  // generate local image url baesd on name of remote image // assumes remote images has unique names - (nsurl*)getlocalurlforimageurl:(nsurl*)imgurl{     // saving offline copy of data.     nsfilemanager *filemanager = [nsfilemanager defaultmanager];     nsarray *paths = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes);     nsstring *cachesdirectory = [paths objectatindex:0];     nsstring *folderpath = [cachesdirectory stringbyappendingpathcomponent:cach_images_folder];     bool isdir;     // create folder not exist     if (![filemanager fileexistsatpath:folderpath isdirectory:&isdir]){         nserror *dirwriteerror = nil;         if (![filemanager createdirectoryatpath:folderpath withintermediatedirectories:yes attributes:nil error:&dirwriteerror]){             nslog(@"error: failed create folder!");         }     }      nsstring *imgname = [[[imgurl path] lastpathcomponent] stringbydeletingpathextension];      nsurl *cachesdirectoryurl = [[nsfilemanager defaultmanager] urlfordirectory:nscachesdirectory indomain:nsuserdomainmask appropriateforurl:nil create:no error:nil];     nsstring *pathstring = [nsstring stringwithformat:@"%@/%@", cach_images_folder, imgname];     return [cachesdirectoryurl urlbyappendingpathcomponent:pathstring]; }   /**  removes folder contating cahced images,  folder reacreated whenever new image being saved permanent cache  */ -(void)clearimagecache{     // set directory path     nsfilemanager *filemanager = [nsfilemanager defaultmanager];     nsarray *paths = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes);     nsstring *cachesdirectory = [paths objectatindex:0];     nsstring *folderpath =  [cachesdirectory stringbyappendingpathcomponent:cach_images_folder];     bool isdir;     nserror *direrror = nil;     // folder exist     if ([filemanager fileexistsatpath:folderpath isdirectory:&isdir]){         if (![filemanager removeitematpath:folderpath error:&direrror])         nslog(@"failed remove folder");     } }  @end 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -