【问题标题】:Loading image into webview将图像加载到 webview
【发布时间】:2011-10-20 14:20:47
【问题描述】:

如何将本地图像加载到使用

调用的 UIWebView 中的 html 页面中
[self.webView loadHTMLString:htmlSourceCode baseURL:externPageUrl];

我知道我可以通过更改 baseURL 来获取图像。但是,就我而言,这是不可能的。 我还尝试通过在 html 中添加 file://pathToImage/myimage.png 来做到这一点,但如果不更改 baseURL,这将无法正常工作。

有什么办法不用处理shouldStartLoadWithRequest方法?

【问题讨论】:

    标签: iphone uiwebview nsurl


    【解决方案1】:

    只要把图片src设置成图片文件的名字,假设和本地HTML文件在同一个文件夹里。

    【讨论】:

    • 并将其设置为名称,甚至完整路径都不起作用,因为 baseURL
    • 哦,好的,我明白了。我相信 file:// 路径是正确的。根据this page,您可能必须将其设为 file:///users/name/library... 注意额外的斜杠。
    • 我试过了。我还尝试了 / 和 // 和 //// 并用 %20 替换空格并将“/”替换为“//”。 “file://”有效,但前提是我替换了 bundle =(
    【解决方案2】:

    我通过将图像字节直接加载到 html 中来解决它:

    //retrieve image data, to embedd into the html as base64
    NSURL *imgURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"] isDirectory:NO];                  
    NSData *data = [NSData dataWithContentsOfURL:imgURL];
    
    [htmlContent appendFormat:@"<div class=\"myCssClass\" style=\"background:url(data:image/png;base64,%@) no-repeat center center;\" ></div>", 
    [data encodeBase64] ];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多