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

拥有回忆 提交于 2019-12-02 01:05:14
NSURL *url=[[NSBundle mainBundle] bundleURL];
[webView loadHTMLString:string baseURL:url];

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

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);

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

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.

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