我有一个UIWebview加载一个简单的图片。用户需要看到整个图片,而无需垂直滚动。使用setSalesPageToFit:YES,用户已加载图像但需要垂直滚动才能看到整个图片,如何强制图片完全显示而不需要滚动可以使用?
这是我的代码片段:
NSString *pathImg = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"1"] ofType:@"png"];
NSString* webViewContent = [NSString stringWithFormat:
@"<html>"
"<body style='background-color: transparent' >"
"<div align='center'>"
"<img src=\"file://%@\"/ style='align:center;'>"
"</div>"
"</body></html>", pathImg];
webView.backgroundColor =[UIColor clearColor];
[self->webView loadHTMLString:webViewContent baseURL:nil];
[webView setScalesPageToFit:YES];
任何帮助都将受到高度赞赏......
答案 0 :(得分:3)
试试这个..希望它能帮到你
NSString *htmlString = [NSString stringWithFormat:
@"<html>"
"<head>"
"<script type=\"text/javascript\" >"
"function display(img){"
"var imgOrigH = document.getElementById('image').offsetHeight;"
"var imgOrigW = document.getElementById('image').offsetWidth;"
"var bodyH = window.innerHeight;"
"var bodyW = window.innerWidth;"
"if((imgOrigW/imgOrigH) > (bodyW/bodyH))"
"{"
"document.getElementById('image').style.width = bodyW + 'px';"
"document.getElementById('image').style.top = (bodyH - document.getElementById('image').offsetHeight)/2 + 'px';"
"}"
"else"
"{"
"document.getElementById('image').style.height = bodyH + 'px';"
"document.getElementById('image').style.marginLeft = (bodyW - document.getElementById('image').offsetWidth)/2 + 'px';"
"}"
"}"
"</script>"
"</head>"
"<body style=\"margin:0;width:100%;height:100%;\" >"
"<img id=\"image\" src=\"%@\" onload=\"display()\" style=\"position:relative\" />"
"</body>"
"</html>",image path];
[webView setClipsToBounds:YES];
webView.opaque=NO;
webView.backgroundColor=[UIColor clearColor];
[webView setUserInteractionEnabled:NO];
[webView sizeToFit];
[webView setScalesPageToFit:YES];
[webView loadHTMLString:htmlString baseURL:nil];