调整uiwebview中html文件中的img大小
来源:互联网 发布:免费配音软件 编辑:程序博客网 时间:2024/05/04 13:26
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 0
- 调整uiwebview中html文件中的img大小
- html文件中img的路径
- 如何调整html 中 input里面的输入光标大小
- webView 在解析html中自动调整大小
- html调整img的水平垂直位置
- iphone 项目中如何加载本地html文件到uiwebview
- iphone 项目中如何加载本地html文件到uiwebview
- 24.html中img标签中的绝对定位
- UIWebView加载本地HTML文件
- UIwebview加载本地html文件
- UIWebView加载本地HTML文件
- UIWebview加载本地html文件
- UIWebView改变HTML中图片的大小,进行等比例缩放
- 3-HTML中的img标签
- 如何调整System.img 大小,从而多内置应用
- android 如何调整System.img 大小,从而多内置应用
- 嵌入式linux中如何查看ramdisk.img中的文件内容
- HTML中img路径问题
- 每个worker的cycle来自master进程的cycle
- zuchengyuanli
- gnu编译C++11,最好与win32公用代码
- 第十三周项目五:去除空格
- Triangle
- 调整uiwebview中html文件中的img大小
- java工程添加web属性
- Java多线程-BlockingQueue
- 代码手写UI,xib和StoryBoard间的博弈
- No package '*' found
- POJ 1948 Triangular Pastures (线性dp)
- linux命令之ls命令的简明讲解
- 利用Jsoup解析器抓取网页源代码
- k-means 算法matlab实现