为什么在使用UIWebView加载HTMLString时,UIWebView的工作如此缓慢?

问题描述:

在我的APP中, loadHTMLString 大约需要10秒钟,谁能指出问题所在?

It take about 10s to loadHTMLString in my APP, who can point out what's the problem?

- (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];
}

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

After enter the view, If I receive message:

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

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

getHtmlDesc method will return the html string quickly.

从加载日志中,您可以看到加载大约需要5秒钟.

from load log, you can see it take about 5s to load.

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

2012-11-26 20:34:35.322 test[8456:c07] ====start load 2012-11-26 12:34:35 +0000:

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

2012-11-26 20:34:39.890 test[8456:c07] ====finish load 2012-11-26 12:34:39 +0000:

以下是UIWebViewDelegate方法:

Following is UIWebViewDelegate method:

- (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]);
}

请帮帮我,为什么使用 UIWebView loadHTMLString 时,它这么慢?

Please give me a hand, why it is so slow when loadHTMLString with UIWebView?

如果没有什么要渲染的内容,则可能是由于慢速磁盘操作(当您读取html字符串时所执行的操作)所致.

If there is nothing heavy to render, then it's possible that all about slow disk operations, that are performing when you're reading your html string.

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