0

Using Swift with WebView:

I am creating html which I display in the webView control. The text html displays as expected but I am having trouble displaying and image which is in the project.(bundle) below is the code I have tried:

let urlpath = Bundle.main.path(forResource: "ld", ofType: "png");

webOut += "<img src='\(urlpath!)' height='16' width='16'>"
webOut += "<h3 style='color:#FD7D70ff'>\(sta.person!)</h3>"
webOut += "<div style='font-size: \(statementTextSize)'>\(sta.text!)</div>"

let requesturl = NSURL(string: urlpath!)
webView.loadHTMLString(webOut, baseURL: requesturl as URL?)

the image does not display.

What is the best way to display an image in this scenario?

1 Answer 1

1

You have two declarations for urlpath in your code snippet above - so I assume one was added in by mistake. But if I remove that and run your code, it runs fine and displays an image when I test here. So you might want to set a breakpoint in your code just before you call loadHTMLString and see what the HTML you are loading looks like and if perhaps it's valid HTML?

Sign up to request clarification or add additional context in comments.

2 Comments

I removed the extra declaration and the html still display fine but the image doesn't show
Since the code does work here, I can only assume that the issue is something else. Is it possible for you to share the project with me so that I can take a look to see what is going on?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.