2012-07-06 36 views
3

我加載一個HTML文件在UIWebview.I我試圖調整UIWebview中的圖像大小加載。在此web視圖中,根據設備大小將html內容拆分爲多個頁面(下面的代碼顯示)。對於我使用的滑動手勢分頁。它正常工作,直到this.In這個html文件我有幾個圖像的大小互不相同。有些圖像不僅僅是設備的寬度。我需要修正圖像的寬度,使其適合屏幕。我不想滾動webview,因爲我使用了滑動手勢進行分頁。那麼,如何根據設備寬度重新調整圖像的大小。如何在UIWebview中調整圖像大小?

我用下面的代碼

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


    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;')", webview4epub.frame.size.height, webview4epub.frame.size.width]; 
    NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"]; 

    [webview4epub stringByEvaluatingJavaScriptFromString:varMySheet]; 
    [webview4epub stringByEvaluatingJavaScriptFromString:addCSSRule]; 
    [webview4epub stringByEvaluatingJavaScriptFromString:insertRule1]; 
    [webview4epub stringByEvaluatingJavaScriptFromString:insertRule2]; 
    NSString* foundHits = [webview4epub stringByEvaluatingJavaScriptFromString:@"results"]; 
    NSMutableArray* objects = [[NSMutableArray alloc] init]; 

    NSArray* stringObjects = [foundHits componentsSeparatedByString:@";"]; 
    for(int i=0; i<[stringObjects count]; i++){ 
     NSArray* strObj = [[stringObjects objectAtIndex:i] componentsSeparatedByString:@","]; 
     if([strObj count]==3){ 
      [objects addObject:strObj]; 
      } 
     } 


    int totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] intValue]; 

} 

回答

0

此大小的頭圖像以90%的網頁視圖高度:

NSString *imgHeightJSString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('img')[0].style.maxHeight = '%fpx'", webView.bounds.size.height*0.90]; 
[webView stringByEvaluatingJavaScriptFromString:imgHeightJSString]; 

這90%IMAGESIZE無論什麼view.size:

[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('img')[0].style.maxHeight = '90%%'"] ; 

設置widht只需使用maxWidth而不是maxHeight。