Search code examples
iosuiwebviewasset-catalog

How to access image resource in asset catalog from UIWebView (or WKWebView)


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)

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?


Solution

  • 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.