【问题标题】:How to stop loading images in UIWebView?如何停止在 UIWebView 中加载图像?
【发布时间】:2012-03-20 08:47:15
【问题描述】:

我想用 UIWebView 加载一个 html url,但不要在 html 中加载图像。我希望 UIWebView 只在 html 中显示纯文本。而且由于某种原因,我无法在加载之前更改 html 内容。

有什么方法可以实现吗?

【问题讨论】:

标签: iphone html ipad uiwebview


【解决方案1】:
- (NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request
{
    NSURL *url = [request URL];
    BOOL blockURL = [[FilterMgr sharedFilterMgr] shouldBlockURL:url];
    if (blockURL) {
        NSURLResponse *response =
              [[NSURLResponse alloc] initWithURL:url
                                        MIMEType:@"text/plain"
                           expectedContentLength:1
                                textEncodingName:nil];

        NSCachedURLResponse *cachedResponse =
              [[NSCachedURLResponse alloc] initWithResponse:response
                             data:[NSData dataWithBytes:" " length:1]];

        [super storeCachedResponse:cachedResponse forRequest:request];

        [cachedResponse release];
        [response release];
    }
    return [super cachedResponseForRequest:request];
}

来自this的部分代码

希望对你有帮助

【讨论】:

    【解决方案2】:
    Then remove all image tages using below code then after load your html string into webview
    
    
    NSString *str =@"<ul><img src=\"some url\"/><li>Tiny pores in leaves or stems, which water and gas can pass. </li><li>Plants can reduce the amount of water that is passed by closing the stomates during the hottest parts of the day or curling their leaves up to reduce exposure.</li></ul>";
        NSLog(@"before--%@",str);
        NSRange r;
        while ((r = [str rangeOfString:@"<img[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
            str = [str stringByReplacingCharactersInRange:r withString:@""];
    
        NSLog(@"after---%@",str);
    

    【讨论】:

    • 有一些方法可以在源中没有静态 img 标签的情况下加载图像,包括通过 JavaScript 和 CSS。
    猜你喜欢
    • 2011-02-05
    • 1970-01-01
    • 2015-12-07
    • 2012-04-24
    • 2012-10-22
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 2013-10-13
    相关资源
    最近更新 更多