Xcode:带有图像的 WebView 和本地 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11404193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:49:52  来源:igfitidea点击:

Xcode: WebView & local HTML with images

htmlxcodeimageuiwebview

提问by Pantelis Proios

I am trying to load an html file with a folder containing images, in a webView. I followed different tutorials without successfully loading the images. The latest tutorial i saw uses the code above:

我正在尝试在 webView 中加载一个带有包含图像的文件夹的 html 文件。我遵循了不同的教程,但没有成功加载图像。我看到的最新教程使用了上面的代码:

NSString *path = [[NSBundle mainBundle] pathForResource:@"info1" ofType:@"html" inDirectory:@"mainInfo"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

However it gives me a SIGABRT if i use the inDirectoryWithout it it runs but without loading the image. Can you pls help me out with this?

但是,如果我使用inDirectory没有它运行但不加载图像,它会给我一个 SIGABRT 。你能帮我解决这个问题吗?

回答by garretriddle

I can't be 100% sure, but my guess is that you should do the following. Don't use inDirectory, clearly your files are directly in the bundle. (If when you copied the files in you did it by reference, then you would need it).

我不能 100% 确定,但我的猜测是您应该执行以下操作。不要使用 inDirectory,显然您的文件直接在包中。(如果您复制其中的文件时是通过引用进行的,那么您将需要它)。

You should make sure that your html references the images as if they're in the same directory, so <img src="img_name.jpg">.And NOT <img src="folder/img_name.jpg">

您应该确保您的 html 引用图像,就好像它们在同一目录中一样,因此<img src="img_name.jpg">.而不是<img src="folder/img_name.jpg">

The next thing you should do is check to make sure the files are part of your bundle and not executable code. So go to your "target -> build phases -> compile sources" and make sure any html or javascript files ARE NOT in there. Then go down to "target -> build phases -> bundle resources" and make sure they ARE all in there.

您应该做的下一件事是检查以确保文件是您的包的一部分,而不是可执行代码。因此,转到“目标 -> 构建阶段 -> 编译源”并确保其中没有任何 html 或 javascript 文件。然后转到“目标 -> 构建阶段 -> 捆绑资源”并确保它们都在那里。

Then run it again. Please let me know if that works for you.

然后再次运行它。请让我知道这是否适合您。