问题描述
我们有一个本地html显示在 UIWebView
中。在那里,我们想要显示资产目录中定义的图像。
We have a local html to show in UIWebView
. And there, we want to show images that are defined in Asset Catalog.
我知道如果我们在主要包中保持图像平坦,我们可以做类似的事情。代码片段就是这样的。
I know that we can do similar thing if we had the image flat in the main bundle. The code snippet would be like this.
webView.loadHTMLString("<img src='target_image.png'/>", baseURL: NSBundle.mainBundle().bundleURL)
但是,我不知道我可以为target_image.png指定什么png文件打包在资产目录中。 (此外,我们希望指定pdf以利用Xcode 6中的矢量图像支持)
However, I am not sure what I can specify for "target_image.png" if the png file is packaged in asset catalog. (Furthermore, we want to specify pdf to take advantage of vector image support in Xcode 6)
有没有人知道如何实现这个目标?
Does anybody have any idea how to achieve this?
推荐答案
由于资产目录作为单个文件存储在捆绑包中,因此无法获取单个资产的URL。
Since the asset catalog is stored in the bundle as a single file, there's no way to get an URL to a singular asset.
最简单的解决方案是不要将资产目录用于您将在网页视图中显示的图像。
The simplest solution is to NOT use the asset catalog for images you'll show in a web view.
如果保留图像在资产目录中是必不可少的,您唯一的选择是将所需资产解包到不同的文件到应用程序包的文档目录,然后引用这些文件。
If keeping the images in the asset catalog is essential, your only option is to "unpack" the required asset(s) into distinct files to the documents directory of the application bundle, and then reference those files.
这篇关于如何从UIWebView(或WKWebView)访问资产目录中的图像资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!