xcode iPhone SDK:UIWebView 停止加载/下载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/586930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone SDK: UIWebView to stop images from loading/downloading
提问by Domness
How can I use the UIWebView in Xcode so that when it loads up pages it DOESN'T download the images (to cause a faster loading page)?
如何在 Xcode 中使用 UIWebView 以便在加载页面时不下载图像(以加快加载页面)?
采纳答案by Rob Napier
UIWebView
is a pale, poor little shadow of WebKit's full WebView
, for which this is easy. -webView:shouldStartLoadWithRequest:navigationType:
only gets called for navigation. It doesn't get called for every request like WebPolicyDelegate
does on mac. With UIWebView
, here's how I would attack this problem:
UIWebView
是 WebKit 的 full 的苍白可怜的小阴影WebView
,这很容易。-webView:shouldStartLoadWithRequest:navigationType:
只会调用导航。它不会像WebPolicyDelegate
在 mac 上那样为每个请求调用。使用UIWebView
,这是我将如何解决这个问题:
Implement -webView:shouldStartLoadWithRequest:navigationType:
and set it to always return NO
. But you'll also take the request and spawn an NSURLConnection
. When the NSURLConnection
finishes fetching the data, you're going to look through it for any IMG tags and modify them to whatever placeholder you want. Then you will load the resulting string into the UIWebView
using -loadHTMLString:baseURL:
.
实现-webView:shouldStartLoadWithRequest:navigationType:
并将其设置为始终返回NO
。但是你也会接受请求并生成一个NSURLConnection
. 当NSURLConnection
完成获取数据时,您将查看它是否有任何 IMG 标签,并将它们修改为您想要的任何占位符。然后您将生成的字符串加载到UIWebView
using 中-loadHTMLString:baseURL:
。
Of course parsing the HTML is not a trivial task on iPhone, and Javascript loaders are going to give you trouble, so this isn't a perfect answer, but it's the best I know of.
当然,在 iPhone 上解析 HTML 并不是一项微不足道的任务,Javascript 加载程序会给您带来麻烦,所以这不是一个完美的答案,但它是我所知道的最好的答案。
回答by roocell
expanding on Rob's answer. I noticed that when loadHTMLString:baseURL: and always returning NO, that webView:shouldStartLoadWithRequest:navigationType: just keeps getting called. (i suspect loadHTMLString invokes another shouldStartLoadWithRequest).
扩展罗布的答案。我注意到当 loadHTMLString:baseURL: 并且总是返回 NO 时, webView:shouldStartLoadWithRequest:navigationType: 只是不断被调用。(我怀疑 loadHTMLString 调用了另一个 shouldStartLoadWithRequest)。
so what I had to do was alternate between returning YES/NO and I used NSScanner to parse the HTML and change src="http://..." to src=""
所以我必须做的是在返回 YES/NO 之间交替,我使用 NSScanner 解析 HTML 并将 src="http://..." 更改为 src=""
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (pageHasNoImages==YES)
{
pageHasNoImages=FALSE;
return YES;
}
NSString* newHtml;
NSString* oldHtml;
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
oldHtml=[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
newHtml=[self.detail scannerReplaceImg:oldHtml]; // my own function to parse HTML
newHtml=[self.detail scannerReplaceAds:newHtml]; // my own function to parse HTML
if (newHtml==nil)
{
NSLog(@"newHtml is nil");
newHtml=oldHtml;
}
[oldHtml release];
pageHasNoImages=TRUE;
[web loadHTMLString:newHtml baseURL:request.URL];
return NO;
}
回答by roocell
does this actually cause the page to load faster? it sounds like the images are still being downloaded, but we're just not feeding them to the UIWebView.
这实际上会导致页面加载速度更快吗?听起来图像仍在下载中,但我们只是没有将它们提供给 UIWebView。
or does shouldStartLoadWithRequest just load the HTML text first?
还是 shouldStartLoadWithRequest 只是先加载 HTML 文本?
回答by Marco
Be the delegate for the UIWebView, then intercept the call:
成为 UIWebView 的委托,然后拦截调用:
– webView:shouldStartLoadWithRequest:navigationType:
Check the values of navigationType in the documentation. I believe you'll be best served by returning NO
on navigationType == UIWebViewNavigationTypeOther
.
检查文档中的 navigationType 值。我相信你会被返回来提供最好的服务NO
上navigationType == UIWebViewNavigationTypeOther
。