开发者

How to get path to add number of Images that are in the resource to be displayed in the WebView in objective-c

I am added image to my application but I want to display them into my web view. I tried but not succeeded. how can I set the path for thos开发者_开发百科e images in html in image tag and how to display them.

Please help me out of this.

Thank you, Madan Mohan.


Are you just displaying the images individually or do you have an HTML file that references them?

To get a URL to the files directly:

NSString *pathForImage = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
NSURL *URL = [NSURL fileURLWithPath:pathForImage];

If you're loading an HTML file that references the images (such as by including an <img src="image.png">, with no path info) then:

[webView loadHTMLString:htmlString
    baseURL:[[NSBundle mainBundle] resourceURL]];

EDIT: so, supposing you had image.png in your application bundle as a resource, then you could do:

[webView loadHTMLString:@"<html><body><img src=\"image.png\"></body></html>"
    baseURL:[[NSBundle mainBundle] resourceURL]];


In your HTML for the webview do

<img src="imagename">

Where imagename is the filename of the image. The iPhone doesn't have any folder structures or anything like that. That means that even if you have groups or folders set up in XCode, at runtime they are all lumped into one, giant directory.

If that doesn't work right away, look at what your are setting your base url to be. It might be that you need to set the baseURL to nil to remind the phone to look locally.


The following code sample might fix the problem for you.. Good luck!

NSData * image = [[NSMutableData alloc] initWithContentsOfFile:@"image.png"];
[self.webView loadData:image MIMEType:@"image/png" textEncodingName:nil baseURL:nil];
[image release];
[self.view addSubView:webView];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜