在 UIWebView 中使用 HTML 和本地图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/747407/
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-08-28 23:36:13  来源:igfitidea点击:

Using HTML and Local Images Within UIWebView

htmliosiphoneuiwebviewuikit

提问by Jasarien

I have a UIWebView in my app which I want to use to display an image which will link to another url.

我的应用程序中有一个 UIWebView,我想用它来显示将链接到另一个 url 的图像。

I'm using

我正在使用

<img src="image.jpg" /> to load the image.

The problem is that the image doesn't load (ie. it can't be found) even though it's added as a resource in my project and is copied into the bundle.

问题是即使图像作为资源添加到我的项目中并被复制到包中,它也无法加载(即无法找到)。

I've tried using NSBundle to get the full path of the image and using that and it still doesn't show up in the web view.

我已经尝试使用 NSBundle 获取图像的完整路径并使用它,但它仍然没有显示在 Web 视图中。

Any ideas?

有任何想法吗?

回答by Adam Alexander

Using relative paths or file: paths to refer to images does not work with UIWebView. Instead you have to load the HTML into the view with the correct baseURL:

使用相对路径或文件:引用图像的路径不适用于 UIWebView。相反,您必须使用正确的 baseURL 将 HTML 加载到视图中:

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

You can then refer to your images like this:

然后,您可以像这样引用您的图像:

<img src="myimage.png">

(from uiwebview revisited)

(来自uiwebview 重访

回答by Lithu T.V

Use this:

用这个:

[webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];

回答by Anton

I just ran into this problem too. In my case, I was dealing with some images that were not localized and others that were--in multiple languages. A base URL didn't get the images inside localized folders for me. I solved this by doing the following:

我也刚遇到这个问题。就我而言,我正在处理一些未本地化的图像,而另一些则使用多种语言。基本 URL 没有为我获取本地化文件夹中的图像。我通过执行以下操作解决了这个问题:

// make sure you have the image name and extension (for demo purposes, I'm using "myImage" and "png" for the file "myImage.png", which may or may not be localized)
NSString *imageFileName = @"myImage";
NSString *imageFileExtension = @"png";

// load the path of the image in the main bundle (this gets the full local path to the image you need, including if it is localized and if you have a @2x version)
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageFileName ofType:imageFileExtension];

// generate the html tag for the image (don't forget to use file:// for local paths)
NSString *imgHTMLTag = [NSString stringWithFormat:@"<img src=\"file://%@\" />", imagePath];

Then, use imgHTMLTag in your UIWebView HTML code when you load the contents.

然后,在加载内容时在 UIWebView HTML 代码中使用 imgHTMLTag。

I hope this helps anyone who ran into the same problem.

我希望这可以帮助遇到同样问题的任何人。

回答by Ping

try use base64 image string.

尝试使用 base64 图像字符串。

NSData* data = UIImageJPEGRepresentation(image, 1.0f);

NSString *strEncoded = [data base64Encoding];   

<img src='data:image/png;base64,%@ '/>,strEncoded

回答by Ping

I had a simmilar problem, but all the suggestions didn't help.

我有一个类似的问题,但所有的建议都没有帮助。

However, the problem was the *.png itself. It had no alpha channel. Somehow Xcode ignores all png files without alpha channelduring the deploy process.

但是,问题在于 *.png 本身。它没有 alpha 通道。在部署过程中,Xcode 以某种方式忽略了所有没有 alpha 通道的 png 文件

回答by zeeawan

You can add folder (say WEB with sub folders css, img and js and file test.html) to your project by choosing Add Files to "MyProj"and selecting Create folder references. Now the following code will take care about all the referred images, css and javascript

您可以通过选择Add Files to "MyProj"并选择Create folder references将文件夹(比如带有子文件夹 css、img 和 js 以及文件 test.html 的 WEB)添加到您的项目中。现在以下代码将处理所有引用的图像、css 和 javascript

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"WEB/test.html" ofType:nil];
[webView  loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

回答by MrAn3

In Swift 3:

在 Swift 3 中:

webView.loadHTMLString("<img src=\"myImg.jpg\">", baseURL: Bundle.main.bundleURL)

This worked for me even when the image was inside of a folder without any modifications.

即使图像位于文件夹内而没有任何修改,这对我也有效。

回答by Lasse Christiansen

After having read a couple of chapters in the iOS 6 Programming Cookbok and started to learn objective-c and iOS programming, I would just like to add, that if one is going to load resources from a custombundle and use that in a web view, it can be accomplished like this:

在阅读了 iOS 6 Programming Cookbok 中的几章并开始学习 Objective-c 和 iOS 编程之后,我想补充一点,如果要从自定义包加载资源并在 Web 视图中使用它,它可以这样完成:

NSString *resourcesBundlePath = [[NSBundle mainBundle] pathForResource:@"Resources" ofType:@"bundle"];
NSBundle *resourcesBundle = [NSBundle bundleWithPath:resourcesBundlePath];
[self.outletWebView loadHTMLString:[html description] baseURL:[resourcesBundle bundleURL]];

Then, in your html you can refer to a resource using the "custom" bundle as your base path:

然后,在您的 html 中,您可以使用“自定义”包作为基本路径来引用资源:

body {
    background-image:url('img/myBg.png');
}

回答by Musa almatri

Swift Version of Lithu T.V's answer:

Lithu TV 的 Swift 版回答:

webView.loadHTMLString(htmlString, baseURL: NSBundle.mainBundle().bundleURL)

回答by RJH

Swift version of Adam Alexanders Objective C answer:

Swift 版本的 Adam Alexanders Objective C 答案:

let logoImageURL = NSURL(fileURLWithPath: "\(Bundle.main.bundlePath)/PDF_HeaderImage.png")