load image in web view from a .html file

荒凉一梦 提交于 2019-12-08 10:23:53

问题


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


回答1:


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




回答2:


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?



来源:https://stackoverflow.com/questions/1186883/load-image-in-web-view-from-a-html-file

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