iOS 修改网络图片的大小 宽和高,终于解决了这个问题

本文介绍了如何在iOS中修改网络图片的大小,通过自编写的JavaScript实现,虽然过程有些复杂,但能显著提升图片管理效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//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();"];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值