从字符串缓慢加载UIWebView [英] Slow loading UIWebView from string
问题描述
我正在尝试从字符串中加载 UIWebView
,如下所示:
I am trying to load a UIWebView
from a string as follows:
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];
纯内容包含一些带有约5个链接和400个字符的简单HTML.我试图在我的iPhone5上运行它,并且第一次加载它总是要花几秒钟.有谁知道为什么会这样以及如何解决这个问题?
Where plain content has some simple HTML with about 5 links and 400 characters. I'm trying to run this on my iPhone5, and the first time it loads it always take a few seconds. Does anyone know why this is happening and how to fix this?
推荐答案
这通常是由于渲染网页中使用的 CSS
导致的.这是在本地加载页面时的默认行为.我们还可以考虑在第一次加载时, UIWebview
对此没有缓存,也没有为该页面创建缓存.
This usually happens because of CSS
used in rendering web page. It is default behavior when loading page locally. We can also consider that in first load, UIWebview
doesn't have cache to this and create cache for that page.
要使其快一点,请尝试从文件中加载页面,例如
To make it little fast try loading page from a file e.g.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"filePath" ofType:@"html" inDirectory:@"."]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
CSS
就像 body {background-color:transparent;字体家族:Arial-BoldMT;font-size:18;}
也增加了加载页面的时间.
CSS
like body { background-color:transparent; font-family:Arial-BoldMT; font-size:18;}
also increase the time of loading a page.
这篇关于从字符串缓慢加载UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!