我在UIWebView中显示了少量本地内容,大约3段文本和1张100x100px图像.当UIWebView被推送到导航控制器时,在显示内容之前大约需要半秒钟.这发生在设备上,而不是模拟器中.显然UIWebView需要在显示内容之前做一些工作,但是在推出的视图出现之前我是否有某种方法可以做到这一点?我自己也没有运气.
这是我正在创建和推送包含UIWebView的视图控制器的地方:
ArticleViewController* viewController = [[ArticleViewController alloc] init];
viewController.article = article;
[viewController view]; //touch the view so it gets loaded
[viewController loadWebView]; //load the web view content
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
Run Code Online (Sandbox Code Playgroud)
这是loadWebView方法:
-(void)loadWebView {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString* content = article.content;
NSString *html = [NSString stringWithFormat:@"\
<html><body style='background-color: transparent'><style type=""text/css"">body {font-family: helvetica; color: black; font-size: 10pt; margin: 10px 10px 10px 10px}</style>\
%@<div style='text-align:center'><a href='13443574_9709e4cf37.jpg?photographer=test' style='-webkit-tap-highlight-color:rgba(0,0,0,0);'><img src='13443574_9709e4cf37.jpg' height='160' width='230'></a></div></body></html>", content];
[self.webView loadHTMLString:html baseURL:baseURL];
}
Run Code Online (Sandbox Code Playgroud)
以前我[viewController loadWebView]在viewDidLoad方法中,但结果似乎是一样的.按下viewController时出现空白屏幕,然后半秒后加载内容.
有任何想法吗?
Bas*_*que 11
在我的UIWebView内容出现之前,我也看到了白屏延迟半秒或更长时间.只有在应用程序运行期间首次使用UIWebView时才会发生这种情况.UIWebView的连续出现几乎是即时的.所以在我和其他人看来,延迟必须归因于WebKit库需要加载和初始化.
你无法消除延迟.但是,您可以将延迟移至应用程序的开头,以减轻"空白屏幕"对用户的恼人影响.诀窍是在应用程序启动期间在屏幕外加载虚假页面的UIWebView.在硬编码字符串中构建最小的HTML5网页.我使用正确有效的HTML5内容来最小化UIWebView/WebKit分析所花费的时间.
这种技术在真实硬件(iPhone 3GS)上运行得非常好,而不仅仅是iOS模拟器.
在我的app delegates didFinishLaunchingWithOptions 方法中,符合ARC的代码的底部如下所示:
…
[self.window makeKeyAndVisible];
// Performance optimization: Warm up the UIWebView widget and its related WebKit libraries.
// We are choosing the trade-off between (A) a slight delay here during app launch to create and abandon a bogus UIWebView instance, and
// (B) a flash of white and noticeable delay as the UINavigationController slides in from the right the first UIWebView we display during the app run.
UIWebView* bogusWebView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
NSString* html = @"<!doctype html><html lang=en><head><meta charset=utf-8><title>blah</title></head><body><p>bogus content</p></body></html>";
[bogusWebView loadHTMLString:html baseURL:nil]; // Load the page.
bogusWebView = nil; // Not really needed, but self-documents that we intend to discard this object.
return YES;
Run Code Online (Sandbox Code Playgroud)
在用户首次在屏幕上显示UIWebView时,这种技术似乎减少了大部分但不是全部大约半秒的延迟.我得出结论,大部分延迟是由于WebKit升温,但必须有一些与在屏幕上以图形方式呈现UIWebView相关的开销.如果是这样,我们无法通过屏幕外工作来消除剩余的开销.但是,这种技术消除了大部分的初始延迟.因此,用户对我的应用的第一印象不会"慢".
你会让用户等待,唯一的问题是:是在 webview 出现之前还是之后?如果您设置为“之前”,那么您应该创建控制器,加载 Web 视图,但要等到 -webViewDidFinishLoad: 委托方法触发才推送它。收到后,您可以推送视图。
| 归档时间: |
|
| 查看次数: |
3489 次 |
| 最近记录: |