改善 UIWebView 初始化时间 [英] Improving UIWebView initialization time

查看:62
本文介绍了改善 UIWebView 初始化时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司使用 UIWebView 来展示广告.我遇到的问题是初始化 UIWebView 似乎很昂贵;使用 Time Profiler 进行分析显示 [UIWebView alloc] initWithFrame:CGRectMake(0,0,500,500)] 需要 31–40 毫秒.这足以在以 60 FPS 运行的游戏中导致明显的掉帧.

My company uses a UIWebView to display ads. The issue I'm having is that initializing a UIWebView appears to be expensive; profiling with Time Profiler shows [UIWebView alloc] initWithFrame:CGRectMake(0,0,500,500)] to take 31–40ms. This is enough to cause noticeable frame drops in games running at 60 FPS.

有没有办法解决这个缓慢的初始化时间?我目前的想法是在应用启动时(但在游戏开始之前)创建一个 UIWebView,然后重用它(可能创建一个它们的池来重用,就像 UITableViewCell工作)或尝试看看 WKWebView 是否有更好的性能.

Is there a way to workaround this slow initialization time? My current ideas are to create a UIWebView when the app launches (but before gameplay has started), and reuse that (potentially creating a pool of them to reuse, like how UITableViewCell works) or to try and see if WKWebView has better performance.

推荐答案

以下是我的发现:

  1. WKWebView 不会更快地初始化.创建 WKWebView 花费的时间与创建 UIWebView 的时间相似(在我做的第一个测试中,创建两个 WKWebView 花费了 46 毫秒.
  2. 创建的第一个网络视图比后续的网络视图花费的时间要长得多.第一个需要 31-42 毫秒;后续的创建需要大约 11 毫秒.这里的好消息是,在创建第一个 Web 视图时,例如应用启动后可以更低成本地创建未来的网络视图,避免在游戏运行时出现 40 毫秒的命中.
  3. 为我的用例创建一个 UIWebView 池是一个很好的解决方案.通过在应用启动时创建 Web 视图然后重复使用它们,我可以避免在游戏运行时导致丢帧.
  1. WKWebView does not initialize any faster. Creating WKWebViews took a similar amount of time as creating UIWebViews (in the 1 test I did, it took 46ms to create two WKWebViews.
  2. The very first web view created takes significantly longer to create than subsequent web views. The first one takes 31–42 ms; subsequent ones take ~11ms to create. The good news here is that creating the first web view when e.g. the app launches allows future webviews to be created more cheaply, avoiding a 40ms hit while the game is running.
  3. Creating a pool of UIWebViews was a good solution for my use case. By creating the webviews at app launch and then reusing them, I avoid causing a frame drop while the game is running.

这篇关于改善 UIWebView 初始化时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆