uiimage - iOS can write or read from image data from disk -


i'm trying load images using following method.

i first check if have images on disk, if image data disk , load otherwise image server , write disk second time need image won't have access server.

the problem doesn't seem write or read disk. everytime want load images second time it's still reads them server , nslog(@"disk"); never gets called.

i don't know i'm doing wrong if has idea?

-(uiimage *)imagewith:(nsstring *)imagename ispreview:(bool)preview {      //imagename "56.jpg"       nsstring *mainorpreview = @"preview";      if (!preview) {          mainorpreview = @"main";      }       nsstring *pathsuffix = [[@"images" stringbyappendingpathcomponent:mainorpreview] stringbyappendingpathcomponent:imagename];      nsstring *path = [[nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0] stringbyappendingpathcomponent:pathsuffix];      nsdata *imagedata = [nsdata datawithcontentsoffile:path];       if (imagedata) {          nslog(@"disk");      }       if (!imagedata && [self connected]) {            imagedata = [nsdata datawithcontentsofurl:[nsurl urlwithstring:[serverurl stringbyappendingpathcomponent: pathsuffix]]];           if (imagedata) {               [imagedata writetofile:path atomically:yes];          }           nslog(@"server");      }        return [uiimage imagewithdata:imagedata]; } 

the problem directories don't exist beyond documents. therefore attempts write files failing. it's idea use file methods have nserror parameters can check result.

you need update code writes image server.

if (!imagedata && [self connected]) {     // needs done in on background thread!     imagedata = [nsdata datawithcontentsofurl:[nsurl urlwithstring:[serverurl stringbyappendingpathcomponent: pathsuffix]]];      if (imagedata) {         [[nsfilemanager defaultmanager] createdirectoryatpath:[path stringbydeletinglastpathcomponent] withintermediatedirectories:yes attributes:nil error:nil];         [imagedata writetofile:path atomically:yes];     }      nslog(@"server"); } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -