2

I'm locally loading images into a WKWebView.

I can load local assets by using:

let imgURL = URL(fileURLWithPath: Bundle.main.bundlePath, isDirectory: true)
        webView.loadHTMLString(html, baseURL: imgURL)

and reference from my HTML string,

but how can I do the same for an asset in the asset catalogue

<img src="variable" alt="variable">

does not work even though "variable" sits within the asset catalogue..

WishIHadThreeGuns
  • 1,225
  • 3
  • 17
  • 37
  • The asset catalog is a single file. Images are buried in it in some impenetrable format. There are no image "files" anywhere in the story. So there can be no URLs. Besides, an iOS app is sandboxed; it can only see where it's allowed to see. – matt Jul 29 '19 at 04:03
  • Duplicate of https://stackoverflow.com/questions/21769092/can-i-get-a-nsurl-from-an-xcassets-bundle ? – Martin R Jul 29 '19 at 04:04

1 Answers1

2

As you rightly imply, an image in the asset catalog has no URL. So what you are suggesting is impossible. I suppose you could fetch an image from the asset catalog and write it out to disk so that now it has a file URL... But it would probably be simpler to keep the image in the bundle, as in your first example.

matt
  • 515,959
  • 87
  • 875
  • 1,141