UIWebView设置图片的宽度适应屏幕的大小

热门标签

,

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

#import "ViewController.h"
 
@interface ViewController ()<UIWebViewDelegate>
 
@property (weak, nonatomic) IBOutletUIWebView *webView;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [superviewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
 
- (void)uiConfig
{
    self.webView.delegate =self;
}
 
#pragma mark - 设置图片的宽度适应屏幕的大小
- (void)setWebViewHtmlImageFitPhone
{
    CGFloat width = [[UIScreenmainScreen]bounds].size.width;
    
    NSString *jsStr = [NSStringstringWithFormat:@"var script = document.createElement('script');"
                     "script.type = 'text/javascript';"
                     "script.text = \"function ResizeImages() { "
                     "var myimg,oldwidth;"
                     "var maxwidth = '%f';" //自定义宽度
                     "for(i=0;i <document.images.length;i++){"
                     "myimg = document.images[i];"
                     "if(myimg.width > maxwidth){"
                     "oldwidth = myimg.width;"
                     "myimg.width = maxwidth;"
                     "}"
                     "}"
                     "}\";"
                     "document.getElementsByTagName('head')[0].appendChild(script);",width];
    [_webViewstringByEvaluatingJavaScriptFromString:jsStr];
    [_webViewstringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}
 
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"开始加载");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"加载完成");
    [selfsetWebViewHtmlImageFitPhone];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullableNSError *)error
{
    NSLog(@"加载失败 error == %@",error);
}
 
- (void)didReceiveMemoryWarning {
    [superdidReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end

 

标签:

未经允许不得转载:作者:SheaYang, 转载或复制请以 超链接形式 并注明出处 技术Dog|博客
原文地址:《UIWebView设置图片的宽度适应屏幕的大小》 发布于2019-10-31

分享到:
赞(0) 打赏

评论 抢沙发

2 + 2 =


UIWebView设置图片的宽度适应屏幕的大小

长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu4.0主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?