【问题标题】:Why UIWebView work so slowly when loadHTMLString with UIWebView?为什么使用 UIWebView 加载 HTMLString 时 UIWebView 工作如此缓慢?
【发布时间】:2012-11-13 22:23:59
【问题描述】:

我的APP中loadHTMLString大概需要10s左右,谁能指出是什么问题?

- (void)viewDidLoad {
    [super viewDidLoad];
    webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH, 480)];
    [webView setBackgroundColor:[UIColor clearColor]];
    [webView setOpaque:NO];
    webView.delegate = self;
    [scrollView addSubview:webView];
}

进入视图后,如果收到消息:

- (void)updateJournalView:(NSArray *)journals {
    NSString *descHtml = [self getHtmlDesc:journals];
    [webView loadHTMLString:descHtml baseURL:nil];
 }

getHtmlDesc 方法将快速返回 html 字符串。

从加载日志可以看到加载大约需要5s。

2012-11-26 20:34:35.322 测试[8456:c07] ====开始加载 2012-11-26 12:34:35 +0000:

2012-11-26 20:34:39.890 测试[8456:c07] ====完成加载 2012-11-26 12:34:39 +0000:

以下是 UIWebViewDelegate 方法:

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
 CGRect frame = aWebView.frame;
 frame.size.height = 1;
 aWebView.frame = frame;
 CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
 frame.size = fittingSize;
 aWebView.frame = frame;
 scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, fittingSize.height);
 NSLog(@"====finish load %@:", [NSDate date]);
}

  - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest    *)request
navigationType:(UIWebViewNavigationType)navigationType {
  NSURL *url = [request URL];
  if ([[url scheme] isEqualToString:@"redirect"] &&
  [[url host] isEqualToString:@"opp_detail"]) {
  NSString *oppIdStr = [[url path] stringByReplacingOccurrencesOfString:@"/player_id=" withString:@""];
  if (oppIdStr && [oppIdStr length]) {
    NSNumber *oppId = [NSNumber numberWithInt:[oppIdStr intValue]];
    if ([oppId intValue] != gcontext.me.id) {
      [screenNav gotoOppDetailScreen:[oppId intValue]];
    }
  }
 }
 return YES;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
 NSURL *url = [request URL];
 if ([[url scheme] isEqualToString:@"redirect"] &&
  [[url host] isEqualToString:@"opp_detail"]) {
  NSString *oppIdStr = [[url path] stringByReplacingOccurrencesOfString:@"/player_id="   withString:@""];
 if (oppIdStr && [oppIdStr length]) {
  NSNumber *oppId = [NSNumber numberWithInt:[oppIdStr intValue]];
  if ([oppId intValue] != gcontext.me.id) {
    [screenNav gotoOppDetailScreen:[oppId intValue]];
  }
}
}
return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
  NSLog(@"====start load %@:", [NSDate date]);
}

请帮帮我,为什么loadHTMLStringUIWebView 会这么慢?

【问题讨论】:

  • 请不要在 ScrollView 中添加 webView,因为 WebView 默认包含 ScrollView。如果内容太长而无法以可见的 webView 大小表示,则 webView 将自动使用 ScrollView 来表示内容

标签: iphone html ios uiwebview


【解决方案1】:

如果没有什么重物需要渲染,那么很可能是磁盘操作缓慢,在您读取 html 字符串时正在执行。

https://stackoverflow.com/a/3400892/1851930

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-07
    • 2013-08-18
    • 2011-01-02
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多