【问题标题】:Images not showing in device for Web view in iphone图像未显示在 iPhone 中的 Web 视图设备中
【发布时间】:2011-08-20 00:27:12
【问题描述】:

我正在开发一个应用程序。我遇到了一个问题。我有 HTML 页面,一些图片在资源目录中。每当我在模拟器上运行应用程序时,它都能正常工作。但是当我在设备上运行应用程序时,图像没有显示,但文本只显示,而不是图像问号显示。

代码: NSString *path = [[NSBundle mainBundle] pathForResource:@"test1" ofType:@"html"]; NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];

NSString *htmlString = [[NSString alloc] initWithData: [readHandlereadDataToEndOfFile]encoding:NSUTF8StringEncoding];
 NSURL *baseurl = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseurl];

HTML 代码:

  <img src="mainGrid.png">

【问题讨论】:

    标签: iphone


    【解决方案1】:

    将您的 HTML 代码更改为(file:// 表示它是本地资源):

    <img src='file://%@'>
    

    然后这样使用:

    NSString *pathToLocalImage = [[NSBundle mainBundle] pathForResource:@"mainGrid" ofType:@"jpg"];
    NSString *content = [NSString stringWaithFormat:htmlString, pathToLocalImage];
    [self.webView content baseURL:nil];
    

    【讨论】:

    • 我已经检查过了——它对我有用。尝试从设备中删除应用程序,清除项目,检查图像是否已添加到项目资源中,重新构建并重新安装。
    猜你喜欢
    • 1970-01-01
    • 2011-02-16
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2018-12-26
    相关资源
    最近更新 更多