Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从UIwebview加载图像失败_Ios_Uiwebview - Fatal编程技术网

Ios 从UIwebview加载图像失败

Ios 从UIwebview加载图像失败,ios,uiwebview,Ios,Uiwebview,我正在使用Webview加载存储在我的应用程序库目录中的图像文件,首先我尝试使用resourcePath和bundle path NSString * html = [[NSString alloc] initWithFormat:@"<img src=\"file://%@\"/>", filename]; [self.webView loadHTMLString:newhtml baseURL:[NSURL fileURLWithPath:[[NSBundle mainBund

我正在使用Webview加载存储在我的应用程序库目录中的图像文件,首先我尝试使用resourcePath和bundle path

NSString * html = [[NSString alloc] initWithFormat:@"<img src=\"file://%@\"/>", filename];
[self.webView loadHTMLString:newhtml  baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];

但是如果我在html中设置了图像文件的绝对路径,一切都正常,我想知道为什么?

看看这篇文章:

上面的答案清楚地表明,使用file:path引用图像不适用于UIWebView

您所需要做的就是传递basepath。例如:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];

然后只需要像这样引用它:

提示:记录生成的html字符串,并查看原因。我在html中获得了错误的文件名。。如果我将映像目录正确地放入baseURL中就可以了:NSArray*path=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);NSString*path=[path objectAtIndex:0];路径=[path stringByAppendingPathComponent:@“/ImageCache”];我看到了那篇文章,但它对我没有好处。在我的例子中,我的图像文件存储在bundle路径中,它存储在./Library/Caches/您是否尝试删除file关键字?因为baseURL将处理它。谢谢,问题是html中的文件名和目录不一样…,无论如何,答案帮助,谢谢
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];