调整uiwebview中html文件中的img大小

31 篇文章 0 订阅
28 篇文章 0 订阅

refs:

http://www.cocoachina.com/bbs/read.php?tid-251513-keyword-uiwebview.html

http://www.cocoachina.com/bbs/read.php?tid=254790&page=e&#a


1)通过动态添加js到html文件中,并在适当的时候调用。

//拦截网页图片  并修改图片大小 

[webView stringByEvaluatingJavaScriptFromString:  

 @"var script = document.createElement('script');"   
 "script.type = 'text/javascript';"   
 "script.text = "function ResizeImages() { "   
     "var myimg,oldwidth;"  
     "var maxwidth=380;" //缩放系数   
     "for(i=0;i <document.images.length;i++){"   
         "myimg = document.images;"  
         "if(myimg.width > maxwidth){"   
             "oldwidth = myimg.width;"   
             "myimg.width = maxwidth;"   
             "myimg.height = myimg.height * (maxwidth/oldwidth);"   
         "}"   

     "}"   

 "}";"   

 "document.getElementsByTagName('head')[0].appendChild(script);"];   

[webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];  

隐藏图片,只显示文字

[webViewstringByEvaluatingJavaScriptFromString:
@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = "function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth=380;"//缩放系数
"for(i=0;i <document.images.length;i++){"
"myimg = document.images;"
"myimg.style.display = 'none’;”//隐藏图片
"}"
"}";"
"document.getElementsByTagName('head')[0].appendChild(script);"];

[webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"]; 


2)之前遇到过类似问题,通过解析html文件中的img tag来修改的,相比这个方法是比较笨了。

我的原有方法,引用了第三方库 TFHpple(https://github.com/topfunky/hpple)。


+ (NSString *) strImgStrip:(NSString *)strHtml Width:(NSString *)width  Height:(NSString *)height
{
    @try{
        NSRange rang1=[strHtml rangeOfString:@"<img"];
        if(rang1.length==0){
            return strHtml;
        }
        
        NSString *prefixStr=[[NSString alloc]initWithString:[strHtml substringToIndex:rang1.location]];
        
        NSMutableString *imageStr1=[[NSMutableString alloc]initWithString:[strHtml substringFromIndex:rang1.location]];
        
        NSRange rang2=[imageStr1 rangeOfString:@"/>"];
        //some elformter html
        NSString *imageStr=[[NSMutableString alloc]initWithString:[imageStr1 substringToIndex:rang2.location]];
        
        NSString *suffixStr=[[NSString alloc]initWithString:[imageStr1 substringFromIndex:rang2.location+rang2.length]];
        
        
        NSRange rang3=[suffixStr rangeOfString:@"<img"];
        if(rang3.length){
            suffixStr=[self strImgStrip:suffixStr Width:width  Height:height];
        }
        
        NSData *dataTitle=[imageStr dataUsingEncoding:NSUTF8StringEncoding];
        
        TFHpple *xpathParser=[[TFHpple alloc]initWithHTMLData:dataTitle];
        
        NSArray *elements=[xpathParser searchWithXPathQuery:@"//img"];
        
        
        NSMutableString *xmlString=[[NSMutableString alloc] initWithString:@""];
        for (TFHppleElement *element in elements) {
            NSMutableDictionary *elementContent =[[NSMutableDictionary alloc] initWithCapacity:4];
            [elementContent addEntriesFromDictionary:[element attributes]];
            if([elementContent objectForKey:@"width"]){
                [elementContent removeObjectForKey:@"width"];
            }
            
            [elementContent setObject:width forKey:@"width"];
            
            if([elementContent objectForKey:@"height"]){
                [elementContent removeObjectForKey:@"height"];
            }
            
            [elementContent setObject:height forKey:@"height"];
            
            NSString * str=[self dictionaryToXmlstring:elementContent];
            
            NSLog(@"result: %@", str);
            
            [xmlString appendFormat:@"<img %@ >",str];
        }
        
        return [NSString stringWithFormat:@"%@%@%@",prefixStr,xmlString,suffixStr];
    }@catch (NSException *exception ) {
        NSLog(@"exception.name = %@" , exception.name);
        NSLog(@"exception.reason = %@" , exception.reason);
        //如果出错,原路返回
        return strHtml;
    }
    
}

3)web页面切换时禁止当前页面media播放

-(BOOL)hasMedia:(MediaType)mediaType
{
    NSString *str_mediaType = [self getMediaTypeString:mediaType];
    
    __block BOOL hasMediaTag = NO;
    if (![[NSThread currentThread] isMainThread])
    {
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *hasMediaTestString = [NSString stringWithFormat:@"document.documentElement.getElementsByTagName(\"%@\").length",str_mediaType];
            NSString * result = [self stringByEvaluatingJavaScriptFromString:hasMediaTestString];
            hasMediaTag = [result integerValue] >= 1? YES : NO;
            dispatch_semaphore_signal(sema);
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }
    else
    {
        NSString *hasMediaTestString = [NSString stringWithFormat:@"document.documentElement.getElementsByTagName(\"%@\").length",str_mediaType];
        NSString * result = [self stringByEvaluatingJavaScriptFromString:hasMediaTestString];
        hasMediaTag = [result integerValue] >= 1? YES : NO;
    }
    
    return hasMediaTag;
}

- (void)stopMedia:(MediaType)mediaType
{
    NSString *str_mediaType = [self getMediaTypeString:mediaType];
    if (![[NSThread currentThread] isMainThread])
    {
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString * requestDurationString = [NSString stringWithFormat:@"document.documentElement.getElementsByTagName(\"%@\")[0].pause()",str_mediaType];
            [self stringByEvaluatingJavaScriptFromString:requestDurationString];
            dispatch_semaphore_signal(sema);
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }
    else
    {
        NSString * requestDurationString = [NSString stringWithFormat:@"document.documentElement.getElementsByTagName(\"%@\")[0].pause()",str_mediaType];
        [self stringByEvaluatingJavaScriptFromString:requestDurationString];
    }
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值