Load image in local HTML file -- UIWebView(在本地 HTML 文件中加载图像 -- UIWebView)
问题描述
我在 UIWebView 中加载本地 HTML 文件,如下所示:
I load a local HTML file in a UIWebView like so:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];    
在我的 index.html 中,如何使用 img src="..."/> 加载显示本地图像(通过 Xcode 添加到项目中)?
In my index.html, how do I load a display a local image (added to project through Xcode) with img src="..."/> ?
推荐答案
不使用 NSURLRequest 加载,而是将 html 文件读入 NSString 并创建一个 NSURL 到带有图像文件的目录.
Instead of loading with an NSURLRequest, read the html file into an NSString and create an NSURL to the directory with image files.  
然后使用 UIWebView 的 - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL 方法.
Then use UIWebView's - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL method.
所以,你会有类似...
So, you'd have something like...
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:html baseURL:[url URLByDeletingLastPathComponent]];
                        这篇关于在本地 HTML 文件中加载图像 -- UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在本地 HTML 文件中加载图像 -- UIWebView
				
        
 
            
        基础教程推荐
- Play 商店的设备兼容性问题 2022-01-01
 - iOS - UINavigationController 添加多个正确的项目? 2022-01-01
 - UIImage 在开始时不适合 UIScrollView 2022-01-01
 - Android Volley - 如何动画图像加载? 2022-01-01
 - SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
 - navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
 - 如何将图像从一项活动发送到另一项活动? 2022-01-01
 - 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
 - Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
 - 如何比较两个 NSDate:哪个是最近的? 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				