iOS UIWebView 圖片大小調整

在 uiwebView的didfini     中執行函數

#pragma mark 攔截並修改圖片大小

- (void)modifyImageToFitScreen:(UIWebView *)webView

{

    [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"

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

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

     "var myimg,oldwidth;"

     "var maxwidth= document.body.offsetWidth;"

     "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);"];

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


}



這個在網上找到了很多代碼 但是調整都不是很對  主要是因爲

當調整寬度之後 高度js會自定按比例計算圖片的新的高度不用再去計算了 否者就是二次按比例縮放


Javascript:

 

 網頁可見區域寬: document.body.clientWidth

 網頁可見區域高: document.body.clientHeight

 網頁可見區域寬: document.body.offsetWidth (包括邊線的寬)

 網頁可見區域高: document.body.offsetHeight (包括邊線的高)

 網頁正文全文寬: document.body.scrollWidth

 網頁正文全文高: document.body.scrollHeight

 網頁被捲去的高: document.body.scrollTop

 網頁被捲去的左: document.body.scrollLeft

 網頁正文部分上: window.screenTop

 網頁正文部分左: window.screenLeft

 屏幕分辨率的高: window.screen.height

 屏幕分辨率的寬: window.screen.width

 屏幕可用工作區高度: window.screen.availHeight

 屏幕可用工作區寬度: window.screen.availWidth

 


這是蒐集的資料   保留已做記錄   後續有需要直接可以拿過來  

最後附上主要的參考資料 出處:http://zxs19861202.iteye.com/blog/1853102



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章