我正在尝试从字符串中加载UIWebView
,如下所示:
NSString* plainContent = @"...";
NSString* htmlContentString = [[NSString stringWithFormat:
@"<html>"
"<style type=\"text/css\">"
"body { background-color:transparent; font-family:Arial-BoldMT; font-size:18;}"
"</style>"
"<body>"
"<p>%@</p>"
"</body></html>", plainContent] retain];
[webView loadHTMLString:htmlContentString baseURL:nil];
普通内容具有一些简单的HTML,带有大约5个链接和400个字符。我试图在我的iPhone5上运行它,并且第一次加载它总是需要几秒钟。有谁知道为什么会这样以及如何解决这个问题?
最佳答案
这通常是由于渲染网页中使用了CSS
而发生的。这是本地加载页面时的默认行为。我们还可以考虑在第一次加载时,UIWebview
没有对此的缓存并为该页面创建缓存。
为了使其快一点,请尝试从文件中加载页面,例如
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"filePath" ofType:@"html" inDirectory:@"."]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
像
CSS
这样的body { background-color:transparent; font-family:Arial-BoldMT; font-size:18;}
也增加了加载页面的时间。
关于iphone - 从字符串缓慢加载UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18273289/