简体   繁体   中英

iOS Swift Display Xcassets Image in WebView

I can't figure out how to display an image "HappyFace.png" in a WebView when the image is inside the Assets.xcassets folder.

Instead, I'm forced to store the image in the same location (same level) as the Assets.xcassets file and other dot swift files. As long as I store it there, I can refer to it by name:

let htmlString: NSString = "<html><body><img src='HappyFace.png'/></body></html>"
let path: NSString = NSBundle.mainBundle().bundlePath
let baseURL: NSURL = NSURL(fileURLWithPath: path as String)
MyWebView.loadHTMLString(htmlString as String, baseURL: baseURL)

Does anyone know how to access the images in xcassets? Is it even appropriate to store my image there? (It's not an app icon, nor a loading screen image, maybe it doesn't belong under xcassets?).

Snapshot of My File Hierarchy

There is no simple way to do this. You must either store the images outside XCAssets and reference them as you have done, or unpack XCAssets into a local directory.

This question includes a useful summary, and one answer demonstrates how to achieve this using a custom URL protocol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM