UIwebview 加载图片宽度自适应屏幕

UIwebview 加载图片宽度自适应屏幕


#import <UIKit/UIKit.h>

@interface MyDownWebController : UIViewController

@property (nonatomic, strong) NSString *fileUrl;
@property (weak, nonatomic) IBOutlet UIWebView *webview;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end




#import "MyDownWebController.h"

@interface MyDownWebController ()

@end

@implementation MyDownWebController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSURL *url = [NSURL fileURLWithPath:self.fileUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webview loadRequest:request];
//    self.webview.autoresizesSubviews = YES;
self.webview.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)backParentBtnTapped:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}

@end



// webview 加载图片宽度自适应屏幕
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *js = @"function imgAutoFit(){ \
var imgs = document.getElementsByTagName('img'); \
for (var i = 0; i < imgs.length; ++i) { \
var img = imgs[i]; \
img.style.maxWidth = %f; \
} \
}";
js = [NSString stringWithFormat:js,DeviceScreenWidth - 20];
[webView stringByEvaluatingJavaScriptFromString:js];
[webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
}











Loading Disqus comments...
Table of Contents