iphone - 从字符串缓慢加载UIWebView

标签 iphone ios cocoa-touch uiwebview

我正在尝试从字符串中加载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/

相关文章:

iphone - Apple Watch 中的布局

ios - 如何在 iOS 中显示当前和前 6 个月的年份?

ios - UITextField 自动大写类型 - iPhone App

ios - 滑动文本,iOS

iphone - 如何重置 UITableView 的滚动位置?

iphone - 如何将 NSObject 从 NSDictionary 转换为 NSString?

iphone - 使用 NSUserDefaults 存储 iPhone 应用程序的最新状态是个好主意吗?

ios - UIWebView 屏幕适配问题

ios - 如何通过 O365-iOS-Connect 获取/创建日历?

iphone - 远程重新加载 View