Display an image in html file in iphone
Hi All
I want to load an image from HTMl file in IPHone but i m unable to do that plz edit my code for this-UIWebView *webView=[[UIWebView alloc]initWithFrame:CGRectMake(10, 50, 650, 768)];
NSString *path=[[NSBundle mainBundle]pathForResource:@"Rough" ofType:@"html"];
NSString *fileContents=[[NSString alloc]initWithContentsOfFile:path];
[webView loadHTMLString:fileContents baseURL:nil];
[self.view addSubview:webView];
&a开发者_如何转开发mp;.html is-
<HTML>
<BODY>
<img src=”HomePage.png”>
</BODY>
</HTML>
how i can show this image in WebView.
Well, you're setting baseURL to nil. If you want the image to load, surely you have to specify it, or how else does it know where "HomePage.png" is?
Where is HomePage.png stored? On the internet, or in the app bundle? Use a file://
URL if it's stored in the app bundle, specify the proper URL if it's stored online.
UPDATE: Based on your comment, it looks like you're setting the base URL as the top level of the .app bundle.
Complete code as requested.
I'm assuming you have this directory layout:
YourApp.app/
Contents/
Resources/
Rough.html
Images/
HomePage.png
...
Therefore both your baseURL needs to be set to the Resources URL, and the <img>
tag in the HTML needs to have Images/
in the src
attribute.
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *resourcesURL = [[mainBundle bundleURL]
stringByAppendingString:@"Contents/Resources"];
NSString *htmlURL = [resourcesURL stringByAppendingString:@"/Rough.html"];
NSString *htmlString = [NSString stringWithContentsOfURL:htmlURL
encoding:NSUTF8StringEncoding
error:NULL];
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 50, 650, 768)];
[webView loadHTMLString:htmlString baseURL:resourcesURL];
[self.view addSubview:webView];
And the HTML should be:
<html>
<body>
<img src="Images/HomePage.png" alt="Your placeholder text" />
</body>
</html>
(Untested)
PS: Curly quotes “ and “ are NOT the same as ASCII double quotes " and " in almost all programming languages. Be careful about using them.
精彩评论