ios - How to cache image on webview? -
i have uiwebview in uiviewcontroller. i'm loading content uiwebview via function:
- (void)loadhtmlstring:(nsstring *)html { nsstring *path = [[nsbundle mainbundle] bundlepath]; nsurl *baseurl = [nsurl fileurlwithpath:path]; [self.webview loadhtmlstring:html baseurl:baseurl]; }
if content contains many images, create gallery via fgallery (https://github.com/gdavis/fgallery-iphone). when open uiviewcontroller @ first time, i'm loading images , text in uiwebview. close application , turn off network. open app , open uiviewcontroller. i'm loading uiwebview cache. have problem. if opened gallery until turn off network, cover of gallery doesn't load cache, images gallery loaded. if didn't open gallery until turn off network, cover of gallery loaded cache. cover of gallery , first image of gallery same.
why cover of gallery doesn't load if opened gallery?
may suitable answer of webview
- (bool)webview:(uiwebview*)webviewref shouldstartloadwithrequest:(nsurlrequest*)request navigationtype:(uiwebviewnavigationtype)navigationtype { if (navigationtype == uiwebviewnavigationtypelinkclicked) { nsurl *url = [request url]; //get url if ( [[url scheme] isequaltostring:@"objc"] ) { webview = webviewref; sel method = nsselectorfromstring( [url host] ); if ([self respondstoselector:method]) { [self performselector:method withobject:nil afterdelay:0.1f]; } return no; } } return yes; } -(void)takepicture { uiimagepickercontroller *imagepicker = [[uiimagepickercontroller alloc] init]; imagepicker.sourcetype = uiimagepickercontrollersourcetypecamera; imagepicker.delegate = self; id delegate = [[uiapplication sharedapplication] delegate]; [[delegate viewcontroller] presentmodalviewcontroller:imagepicker animated:yes]; [imagepicker release]; } #pragma mark - image picker -(void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { uiimage *image = [info objectforkey:@"uiimagepickercontrolleroriginalimage"]; nsdata *flatimage = uiimagejpegrepresentation(image, 0.1f); nsstring *image64 = [flatimage base64encodedstring]; nsstring *js = [nsstring stringwithformat: @"processimage('data:image/jpeg;base64,%@')", image64]; [webview stringbyevaluatingjavascriptfromstring:js]; [picker dismissmodalviewcontrolleranimated:yes]; } - (void)imagepickercontrollerdidcancel:(uiimagepickercontroller *)picker { //cancel hit inside of camera view [picker dismissmodalviewcontrolleranimated:yes]; }
Comments
Post a Comment