0

我的视图控制器中有一个UIWebView,当我尝试打开一个epub文件时,它显示正确,但之后的每个 epub 都显示相同的封面(第一页),并且其内容不同。

为什么会这样?代码里面有缓存吗?

在章节课

- (void) loadChapterWithWindowSize:(CGRect)theWindowSize fontPercentSize:(int) theFontPercentSize
{
    //  [[NSURLCache sharedURLCache] removeAllCachedResponses];
    fontPercentSize = theFontPercentSize;
    windowSize =theWindowSize;
    // NSLog(@"webviewSize: %f * %f, fontPercentSize: %d", theWindowSize.size.width, theWindowSize.size.height,theFontPercentSize);
    UIWebView* webView = [[UIWebView alloc] initWithFrame:theWindowSize];
    [webView setDelegate:self];
    NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:spinePath]];

    //CODE FOR IGNORING CACHE

    //NSURLRequest* urlRequest ;
    //urlRequest=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:spinePath] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10000];

    //[webView loadRequest:urlRequest];
    NSLog(@"%@",spinePath);//html pahe path
    [[UIApplication sharedApplication] openURL:[NSURL fileURLWithPath:spinePath]];
};

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [webView release];
}

- (void) webViewDidFinishLoad:(UIWebView*)webView
{
    [webView stringByEvaluatingJavaScriptFromString:@"document.open();document.close()"];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    sharedManager=[Mymanager sharedManager];
    NSString *varMySheet = @"var mySheet = document.styleSheets[0];";
    NSString *addCSSRule =  @"function addCSSRule(selector, newRule) {"
    "if (mySheet.addRule) {"
    "mySheet.addRule(selector, newRule);"                               // For Internet Explorer
    "} else {"
    "ruleIndex = mySheet.cssRules.length;"
    "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"   // For Firefox, Chrome, etc.
    "}"
    "}";

    // NSLog(@"w:%f h:%f", webView.bounds.size.width, webView.bounds.size.height);

    NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
    NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
    NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",fontPercentSize];


    [webView stringByEvaluatingJavaScriptFromString:varMySheet];
    [webView stringByEvaluatingJavaScriptFromString:addCSSRule];
    [webView stringByEvaluatingJavaScriptFromString:insertRule1];
    [webView stringByEvaluatingJavaScriptFromString:insertRule2];
    [webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];

    int totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] intValue];
    pageCount = (int)((float)totalWidth/webView.bounds.size.width);

    //NSLog(@"Chapter %d: %@ -> %d pages", chapterIndex, title, pageCount);

    [webView dealloc];
    [delegate chapterDidFinishLoad:self];
}

在我的 viewoccontroller 类中

- (void) chapterDidFinishLoad:(Chapter *)chapter{
i=i+1;

    NSLog(@"iteration%d",i);
    NSLog(@"%d",[loadedEpub.spineArray count]);
    loadingProgressBar=loadingProgressBar+1;
    sharedManager=[Mymanager sharedManager];
    NSLog(@"%d",sharedManager.coverPageloadCount);
    //sharedManager.coverPageloadCount++;
    totalPagesCount+=chapter.pageCount;
    sharedManager.TotalPageNumber=totalPagesCount;
    if(chapter.chapterIndex + 1 < [loadedEpub.spineArray count]){
        [[loadedEpub.spineArray objectAtIndex:chapter.chapterIndex+1] setDelegate:self];
        [[loadedEpub.spineArray objectAtIndex:chapter.chapterIndex+1] loadChapterWithWindowSize:webView.bounds fontPercentSize:currentTextSize];
        [currentPageLabel setText:[NSString stringWithFormat:@"?/%d", totalPagesCount]];
    } 
    else {
        [currentPageLabel setText:[NSString stringWithFormat:@"%d/%d",[self getGlobalPageCount], totalPagesCount]];
        [pageSlider setValue:(float)100*(float)[self getGlobalPageCount]/(float)totalPagesCount animated:YES];
        paginating = NO;
        //NSLog(@"Pagination Ended!");
}

    - (void)webViewDidFinishLoad:(UIWebView *)thewebView{

    // [self TextureModes];



    NSUserDefaults *menuUserDefaults = [NSUserDefaults standardUserDefaults];
    if([menuUserDefaults boolForKey:@"btnM1"]){
        [webView setOpaque:NO];
        [webView setBackgroundColor:[UIColor whiteColor]];
        NSString *jsString2 = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
        [webView stringByEvaluatingJavaScriptFromString:jsString2];

    }
    else{
        NSUserDefaults *userDefaults2 = [NSUserDefaults standardUserDefaults];
        [userDefaults2 setBool:NO forKey:@"btnM1"];
        [userDefaults2 synchronize];

        [webView setOpaque:NO];
        [webView setBackgroundColor:[UIColor blackColor]];
        NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
        [webView stringByEvaluatingJavaScriptFromString:jsString];

    }

    if(sharedManager.textureFlag==1)
    {
        webView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"darkWoodP.png"]];
    }else if(sharedManager.textureFlag==2)
    {
        webView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"paperP.png"]];
    }

    NSString *varMySheet = @"var mySheet = document.styleSheets[0];";

    NSString *addCSSRule =  @"function addCSSRule(selector, newRule) {"
    "if (mySheet.addRule) {"
    "mySheet.addRule(selector, newRule);"                               // For Internet Explorer
    "} else {"
    "ruleIndex = mySheet.cssRules.length;"
    "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"   // For Firefox, Chrome, etc.
    "}"
    "}";


    NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
    NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
    NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", currentTextSize];
    NSString *setHighlightColorRule = [NSString stringWithFormat:@"addCSSRule('highlight', 'background-color: yellow;')"];


    [webView stringByEvaluatingJavaScriptFromString:varMySheet];

    [webView stringByEvaluatingJavaScriptFromString:addCSSRule];

    [webView stringByEvaluatingJavaScriptFromString:insertRule1];

    [webView stringByEvaluatingJavaScriptFromString:insertRule2];

    [webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];

    [webView stringByEvaluatingJavaScriptFromString:setHighlightColorRule];
    NSString *padding = @"document.body.style.padding='30px 30px 30px 30px';";
    NSString *paddingtop = @"document.body.style.padding-top='85px';";
    NSString *paddingbottom = @"document.body.style.margin-bottom='85px';";
    [webView stringByEvaluatingJavaScriptFromString:padding];
    [webView stringByEvaluatingJavaScriptFromString:paddingtop];
    [webView stringByEvaluatingJavaScriptFromString:paddingbottom];
    if(currentSearchResult!=nil){
        //  NSLog(@"Highlighting %@", currentSearchResult.originatingQuery);
        [webView highlightAllOccurencesOfString:currentSearchResult.originatingQuery];
    }


    totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] intValue];
    //  NSLog(@"%d %f",totalWidth,webView.bounds.size.width);
    pagesInCurrentSpineCount = (int)((float)totalWidth/webView.bounds.size.width);
    [self gotoPageInCurrentSpine:currentPageInSpineIndex];
}
4

1 回答 1

0

尝试一个实验:与其重新加载 web 视图,不如释放它并​​用一个新视图替换它(正确地 niling 和设置委托)。如果新的 Web 视图显示相同的内容,则它必须是文件。如果这样可以解决问题,那么您现在有一种解决方法。

于 2013-05-29T11:53:20.547 回答