在Iphone上的UIWebView中加载内容
我想在UIWebView中显示内容,我已将其添加到项目中。(例如图像)在Iphone上的UIWebView中加载内容,iphone,user-interface,uiwebview,Iphone,User Interface,Uiwebview,我想在UIWebView中显示内容,我已将其添加到项目中。(例如图像) 我知道如何在UIWebView中设置文本,但我想添加已添加到项目中的图像或视频。好的,我假设您正在使用loadHTMLString在UIWebView上设置HTML 因此,要引用存储在应用程序包中的图像,您只需找到正确的路径,将其放入img标记中即可。因此,获取路径字符串: NSString * imgPath = [[NSBundle mainBundle] pathForResource:nameOfImage ofTy
我知道如何在UIWebView中设置文本,但我想添加已添加到项目中的图像或视频。好的,我假设您正在使用loadHTMLString在UIWebView上设置HTML 因此,要引用存储在应用程序包中的图像,您只需找到正确的路径,将其放入img标记中即可。因此,获取路径字符串:
NSString * imgPath = [[NSBundle mainBundle] pathForResource:nameOfImage ofType:@"jpg"];
使用图像标记格式化某些HTML:
NSString * html = NSString [[[NSString alloc] initWithFormat:@"<img src=\"file://%@\"/>", imgPath] autorelease];
你只需要将图像添加到你的应用程序资源(上面代码中的nameOfImage)中,它就可以正常工作了 将HTML加载到UIWebView中,如下所示:
// assumes you have a UIWebView called 'webView', and an HTML string called 'html'
NSString* imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
[webView loadHTMLString:html baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//", imagePath]]];
然后,您可以只参考图像的文件名:
<img src="your_image.png" />
您不需要指定放入文件夹的图像的相对路径。编译应用程序时,它们都会进入根目录
信用:我从这里偷了这个:要获取文件URL,请使用NSURL::关键字:“我已将其添加到项目中”类似的问题。
<img src="your_image.png" />