How to refer a local image file from UIWebView's HTML on iPhone?

我是研究僧i 提交于 2019-12-20 03:46:09

问题


I would like to use <img src="temp.jpg" /> in my UIWebView's HTML.
How can I refer to a local file named temp.jpg to save precious loading time?

Of course the obvious choice would be <img src=".\temp.jpg" /> but I have no idea where my root is...


回答1:


NSURL *url=[[NSBundle mainBundle] bundleURL];
[webView loadHTMLString:string baseURL:url];

This let's the root be the app's main bundle.




回答2:


This will create a URL object that contains the location of your file on disk. You can then use this to output a valid file:// URL string that can be used within img tags.

NSURL* fileURL = [[NSBundle mainBundle] URLForResource:@"temp" withExtension:@"jpg"];

NSLog(@"<img src=\"%@\" />", fileURL.absoluteString);



回答3:


i was searching alot as olution and i find this solution working for me in my case i have an local html string and a local image i convert the image into base 64 you may use this website to convert image to base64 encoding

http://www.dailycoding.com/Utils/Converter/ImageToBase64.aspx

and rewrite the html string to have the base64 image in the image tag like the following

    <img src="data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP
C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA
AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J
REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq
ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0
vr4MkhoXe0rZigAAAABJRU5ErkJggg==" alt="Red dot" >

hope is will help for other or maybe get a better solution, best of luck




回答4:


I have no idea where my root is...

UIWebView looks up files relative to the HTML file you loaded in it. So perhaps it will be your app bundle.



来源:https://stackoverflow.com/questions/14739179/how-to-refer-a-local-image-file-from-uiwebviews-html-on-iphone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!