1
我正在開發一個示例iPad應用程序,其中我在UIWebview
中顯示了一些網頁,代碼工作正常。但我認爲這些網頁需要花費太多時間才能加載到UIWebview
.My代碼如下:在webview中多次加載網頁的速度較慢
-(void)createWebView
{
//******************** First Webview*********
firstWebView=[[UIWebView alloc]init];
firstWebView.frame=CGRectMake(10, 50, 350, 470);
firstWebView.delegate=self;
firstIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
firstIndicator.center = CGPointMake(160, 240);
firstIndicator.hidesWhenStopped = YES;
[self.firstWebView addSubview:firstIndicator];
[firstIndicator startAnimating];
firstWebView.backgroundColor=[UIColor grayColor];
NSURL *firstUrl = [NSURL URLWithString:@"http://www.techtree.com"];
NSURLRequest *firstRequestObj = [NSURLRequest requestWithURL:firstUrl];
[firstWebView loadRequest:firstRequestObj];
[self.view addSubview:firstWebView];
//******************* Second Webview*********
secondWebView=[[UIWebView alloc]init];
secondWebView.frame=CGRectMake(405, 50, 350, 470);
secondWebView.delegate=self;
secondIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
secondIndicator.center = CGPointMake(160, 240);
secondIndicator.hidesWhenStopped = YES;
[self.secondWebView addSubview:secondIndicator];
[secondIndicator startAnimating];
secondWebView.backgroundColor=[UIColor grayColor];
NSURL *secondUrl = [NSURL URLWithString:@"http://www.facebook.com"];
NSURLRequest *secondRequestObj = [NSURLRequest requestWithURL:secondUrl];
[secondWebView loadRequest:secondRequestObj];
[self.view addSubview:secondWebView];
//****************** Third Webview************
thirdWebView=[[UIWebView alloc] init];
thirdWebView.frame=CGRectMake(10, 528, 350, 470);
thirdWebView.delegate=self;
thirdIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
thirdIndicator.center = CGPointMake(160, 240);
thirdIndicator.hidesWhenStopped = YES;
[self.thirdWebView addSubview:thirdIndicator];
[thirdIndicator startAnimating];
thirdWebView.backgroundColor=[UIColor grayColor];
NSURL *thirdUrl = [NSURL URLWithString:@"http://www.yahoo.com"];
NSURLRequest *thirdRequestObj = [NSURLRequest requestWithURL:thirdUrl];
[thirdWebView loadRequest:thirdRequestObj];
[self.view addSubview:thirdWebView];
//***************** Fourth Webview************
fourthWebView=[[UIWebView alloc] init];
fourthWebView.frame=CGRectMake(405,528, 350, 470);
fourthWebView.delegate=self;
fourthIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
fourthIndicator.center = CGPointMake(160, 240);
fourthIndicator.hidesWhenStopped = YES;
[self.fourthWebView addSubview:fourthIndicator];
[fourthIndicator startAnimating];
fourthWebView.backgroundColor=[UIColor grayColor];
NSURL *fourthUrl = [NSURL URLWithString:@"http://stackoverflow.com"];
NSURLRequest *fourthRequestObj = [NSURLRequest requestWithURL:fourthUrl];
[fourthWebView loadRequest:fourthRequestObj];
[self.view addSubview:fourthWebView];
//******************** Memory Managemt **********
[firstWebView release];
[secondWebView release];
[thirdWebView release];
[fourthWebView release];
}
那麼如何讓它更快,請指導我。
需要多少時間?你是否將實現的性能與在Safari中加載4個不同頁面所需的時間進行比較(我們應該假設這將是最佳性能)。根據我的經驗,將內容加載到WebView中比我想要的要慢,即使內容是本地HTML。你在做什麼看起來非常複雜或錯誤。所以你應該在Safari中進行一些性能測試,以確保你已經正確地設定了對可能性的期望。 – janoside 2012-03-17 14:19:02
那麼,我必須使用NSOperationQueue或任何線程? – user1227928 2012-03-17 14:46:16