【问题标题】:How to show a local image in the webview?如何在 webview 中显示本地图像?
【发布时间】:2009-01-21 08:17:40
【问题描述】:

我想在 webview 中显示一个本地图像,怎么做?有人有一个简单的演示吗?非常感谢!!!

【问题讨论】:

    标签: objective-c cocoa webkit


    【解决方案1】:

    最好的方法是将 baseURL 设置为 Bundles 路径。然后从那里你可以像这样调用你的 Bundle 中的图像:

    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];
    
    [webView loadHTMLString:htmlContent baseURL: bundleBaseURL];
    

    HTML 中的图片可以直接调用本地图片。

    <img src="yourImageFromTheMainBundle.jpg" />
    

    同样可以用于调用 CSS 文件甚至 JS 文件,因为 baseURL 会在主 Bunble 内部查找。

    如果它不在您的主包中,您只需将 baseURL 更改为图像所在的路径。

    【讨论】:

    • [webView loadHTMLString:htmlContent baseURL:[[NSBundle mainBundle] resourceURL]] 为我工作,而上面的例子没有。
    • 我可能听起来有点菜鸟,但请告诉我在哪里添加这个代码段??
    • [webView loadHTMLString:htmlContent baseURL:[[NSBundle mainBundle] resourceURL]] 对我也有用,而上面的例子没有
    【解决方案2】:
    NSURL *imageURL = [NSURL fileURLWithPath:localImagePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];
    [[webView mainFrame] loadRequest:request];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 1970-01-01
      相关资源
      最近更新 更多