0

How can I load an image in iphone webview from a .html file?

2
  • Doesn't the img-element work like in normal HTML? Commented Jul 27, 2009 at 7:27
  • at safari working but not in webview, i wana ur help... Commented Jul 27, 2009 at 7:54

2 Answers 2

4

Easy

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];

    NSLog(@"webview %@", bundlePath);


    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];  
    NSData *htmlData = [NSData dataWithContentsOfFile:filePath];  
    NSString * html =  [NSString stringWithContentsOfFile:filePath encoding:NSStringEncodingConversionAllowLossy error:nil];
    if (htmlData) {  
       [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:bundleBaseURL];  
    }  

Now in your html you can simply refer to the image as

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome can you now accept my answer as and or vote it up. Don't want to miss out on points. Cheers. John.
0

Yes. UIWebView has two methods: -loadHtmlString:baseURL: and -loadData:MIMEType:textEncodingName:baseURL:

You can get the file data using NSData's -initWithContentsOfFile:, then pass it to the latter of those methods above.

EDIT: Whoops, I didn't notice the img part. But it still should work. Are you trying to load only the image?

3 Comments

my images are stored in recourse / images directory, can u send me complete code, me working at this from last 2 days but....? plz...
i m trying .html page, in this table, text, and images. but image is't be loaded.
i m trying to develop an ebook,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.