1、可以是用loadData,这种方法需要先将html文件读取出来,以字符串传入loadData,可以展示页面,但是不会引用css、js等文件。
2、使用loadUrl,不过需要注意,这里因为是使用本地数据,所以传入的url需要做些处理,例如:
a、如果html文件存于assets:则加前缀:file:///android_asset/
b、如果html文件存于sdcard:则加前缀:content://com.android.htmlfileprovider/sdcard/
注意:content前缀可能导致异常,直接使用file:///sdcard/ or file:/sdcard也可以
UIWebView加载工程本地网页与本地图片
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
myWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
myWebView.delegate = self;
[self.view addSubview:myWebView];
[myWebView loadHTMLString:htmlString baseURL:[NSURL URLWithString:filePath]];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSString * js = [NSString stringWithFormat:@"document.images[0].src='file:/%@//%@'",imagePath,@"icon-04.png"];
[myWebView stringByEvaluatingJavaScriptFromString:js];
NSString *path = [myWebView stringByEvaluatingJavaScriptFromString:@"document.images[0].src"];
NSLog(@"path:%@", path);
}