Search code examples
iphoneobjective-cimagewebviewnsbundle

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 those images in html in image tag and how to display them.

Please help me out of this.

Thank you, Madan Mohan.


Solution

  • 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]];