1. 程式人生 > >獲取webView中網頁的高度,並攔截圖片修改圖片的尺寸

獲取webView中網頁的高度,並攔截圖片修改圖片的尺寸

#define ScreenWidth ([UIScreen mainScreen].bounds.size.width)

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

//等比例修改圖片

NSString *str = [NSStringstringWithFormat:@"var script = document.createElement('script');"

"script.type = 'text/javascript';"

"script.text = \"function ResizeImages() { "

                    "var myimg,oldwidth;"

                    "var maxwidth = %f;" // UIWebView中顯示的圖片寬度

"for(i=0;i <document.images.length;i++){"

"myimg = document.images[i];"

"if(myimg.width > maxwidth){"

"oldwidth = myimg.width;"

"myimg.width = maxwidth;"

                    "}"

                    "}"

                    "}\";"

"document.getElementsByTagName('head')[0].appendChild(script);",

ScreenWidth-10];

    [webView stringByEvaluatingJavaScriptFromString:str];

    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];

NSString *webHeight = [webViewstringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"];

   NSLog(@"網頁高度:%@",webHeight);

}