iOS 修改网络图片的大小 宽和高

//image宽和高 好用
NSString *strTemplateHTML = [NSString stringWithFormat:@"<html><head><style>img{max-width:320.0;height:auto !important;width:auto !important;};</style></head><body style='margin:0; padding:0;'>%@</body></html>", @"insert your html content here"];
[webView loadHTMLString:strTemplateHTML baseURL:nil];
//代理执行时调用
网上给的下边的这个方法好像不太好用 我用了 没啥效果
- (void)webViewDidFinishLoad:(UIWebView *)webView1 { //修改服务器页面的meta的值 就是文本的宽高 定义成 系统手机宽高 NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", webView.frame.size.width]; [webView1 stringByEvaluatingJavaScriptFromString:meta]; }

我自己写了一个js ,有点麻烦 但是对图片管理的效果很明显

如下代码:

复制代码
htmlText=[jsonObj objectForKey:@"content"];
jsString = [NSString stringWithFormat:@"<html> \n"
                            "<head> \n"
                            "<style type=\"text/css\"> \n"
                            "body {font-size:%fpx; line-height:%fpx;background-color: transparent;}\n"
                          //  "img{max-width:305;height:auto !important;width:auto !important;};"
                            ".img {width:305.0;}"//关键是这句给所有网络图片设宽度
                            "</style> \n"
                            "</head> \n"
                            "<body>%@</body> \n"
                            "</html>",  fontSize ,line_height,htmlText];
 NSURL *urlBai=[NSURL URLWithString:ImageWeb_Head];
                [showWebView loadHTMLString:jsString baseURL:   urlBai];
                showWebView.delegate=self;

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
       [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
        "var imgs = document.getElementsByTagName('img');"
        
        "for (var i = 0; i < imgs.length; i ++) {"
        " var img = imgs[i];"
        " img.style.width = 305 ;"//图片宽度我设置为305
        " img.style.height = null;"
        "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}
复制代码

 

 

posted on   ACM_Someone like you  阅读(2283)  评论(0编辑  收藏  举报

编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
历史上的今天:
2013-03-13 Uva 11384 - Help is needed for Dexter
2013-03-13 Uva 11300 - Spreading the Wealth

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示