【问题标题】:load image in web view from a .html file从 .html 文件加载 Web 视图中的图像
【发布时间】:2010-11-14 06:51:51
【问题描述】:

如何在 iphone webview 中从 .html 文件加载图像?

【问题讨论】:

  • img 元素不像普通 HTML 那样工作吗?
  • 在 safari 工作,但不在 webview 中,我需要你的帮助...

标签: iphone html uiwebview


【解决方案1】:

简单

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

现在在您的 html 中,您可以简单地将图像称为

【讨论】:

  • 太棒了,您现在可以接受我的回答并或投票。不想错过积分。干杯。约翰。
【解决方案2】:

是的。 UIWebView有两种方法:-loadHtmlString:baseURL:-loadData:MIMEType:textEncodingName:baseURL:

您可以使用 NSData 的-initWithContentsOfFile: 获取文件数据,然后将其传递给上述方法中的后者。

编辑:哎呀,我没有注意到 img 部分。但它仍然应该工作。您是否尝试仅加载图片?

【讨论】:

  • 我的图像存储在资源/图像目录中,你能把完整的代码发给我吗,我从过去 2 天开始就在这工作,但是......?请...
  • 我正在尝试 .html 页面,在此表格中,文本和图像。但图像未加载。
  • 我正在尝试开发电子书,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
  • 1970-01-01
相关资源
最近更新 更多