解决方案 »

  1.   

    是不是图片路径没搞对?在机器上用safari打开这个html,能看到图片吗?
      

  2.   

    看看url在浏览器中是否正常显示。
      

  3.   

    谢谢大家,那天下班回去问了下朋友,直接就出来了。后面就没关注这个帖子。我把代码贴上把,分享下:NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentdirectory = [paths objectAtIndex:0];
        NSString *pathstr = [documentdirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@/%@/",self.paperId,self.plateId,newsId]];
        pathstr = [pathstr stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
        pathstr = [pathstr stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
        NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",pathstr]];
        NSLog(@"baseurl = %@",baseURL);
        NSString *dict1 = [[NSString alloc] initWithString:[UseFileManager getExtractNewsHTMLFile:newsId paperId:self.paperId plateId:self.plateId]];
        NSData *htmlData = [NSData dataWithContentsOfFile:dict1];    NSStringEncoding gb2312 = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
        NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:dict1  encoding:gb2312 error:nil];
        self.urlStr = [NSString stringWithFormat:@"%@%@%@", REQUEST_ROOT_URL, REQUEST_GET_NEWS_DETAIL, newsId];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
    //判断temp文件夹是否存在
    BOOL fileExists = [fileManager fileExistsAtPath:pathstr];
        if (!fileExists) {
            [_mainWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]];
        }else {
            [_mainWebView loadHTMLString:htmlstring baseURL:baseURL];
            //[_mainWebView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"gb2312" baseURL:baseURL];
            NSLog(@"打印图片的路径 = %@",pathstr);
            
        }
    这是整个方法,有遗留代码。我的问题其实就出在路径上,baseURL要处理一下就可以了。
      

  4.   

     baseurl = file:///Users//wiselifebj//Library//Application%20Support//iPhone%20Simulator//6.1//Applications//68DE6353-7CE6-447E-AC03-ED7362B30B2A//Documents//1374721304721//1373931419762//1374011376917//     
    最后打印下这baseUrl;
      

  5.   

    大哥 self.paperId,self.plateId,newsId 这三个参数是什么?有完整的demo 吗 我正好也是遇到这个问题 在浏览器种打开 图片显示 但是用模拟器打开 图片不显示
      

  6.   

    好吧 我已经解决了 由于我的 事例跟楼主的还有所区别 所以 在解决过程种 只能自己试探 并猜测原理 下面说下我的总结 “加载本地html 有资源和没有资源的一样” 
    - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
    解释下参数的意义 
    1 string:html语言文本 
    我用[NSString stringWithContentsOfFile:imageFilePath encoding:NSUTF8StringEncoding error:nil]实例化 
    (imagFilePath :/Users/shiguifeng/Library/Application Support/iPhone Simulator/5.1/Applications/9FCCC772-D91C-4E6C-A980-5CC4D1B8FB65/Library/Caches/index/default.html)
    2 baseURL html 资源路径 (在ios应用程序中 要转化成相对于 html文件的路径)
    在这里 我用终端命令 defaults write com.apple.finder AppleShowAllFiles -bool true 显示了mac的隐藏文件从而通过Finder找到了zip包的位置和解压所得的文件
    下面是我要感谢楼主的地方 通过楼主提供的方法 (在沙盒中的 资源文件用如下的代码)
    pathstr = [pathstr stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
    pathstr = [pathstr stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",pathstr]];
    我顺利的将资源路径转化成相对于html的路径 
    baseURL = file:///Users//shiguifeng//Library//Application%20Support//iPhone%20Simulator//5.1//Applications//9FCCC772-D91C-4E6C-A980-5CC4D1B8FB65//Library//Caches//index//d_img//
    做到这里 一般的问题就该解决了 可以通过上面的加载方法 成功加载html但是我的问题就出现在这里 “图片资源仍然加载不了”
    我开始以为错误 以为是楼主遗漏的代码 中有重要的东西我没加入 比如self.paperId UseFileManager 等。 并且去多个技术网站搜索 但是都失败了 这个时候我 试着读了下html语言 想看看是不是加载的图片名字跟我zip包的图片名字不同导致
    发现其中的图片加载语句都是这种形式<img src="d_img/h.gif"  alt="img"/> 
    我查看了下自己的baseURL
    baseURL = file:///Users//shiguifeng//Library//Application%20Support//iPhone%20Simulator//5.1//Applications//9FCCC772-D91C-4E6C-A980-5CC4D1B8FB65//Library//Caches//index//d_img//
    我又去沙盒中查看了 发现解压后的文件如下
    除了 default.html  还有一个文件夹 d_img 
    问题找到了当系统通过我上面的baseUrl进入d_img文件后 确再也找不到d_img文件了
    然后我果断删除 d_img// baseURL变成了如下
    baseURL = file:///Users//shiguifeng//Library//Application%20Support//iPhone%20Simulator//5.1//Applications//9FCCC772-D91C-4E6C-A980-5CC4D1B8FB65//Library//Caches//index//
    再次运行 果断图片加载图片成功 
    漫漫长路 努力求索
      

  7.   

    [self.webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];