mcfly soft
mcfly soft

Reputation: 11645

best way to show images stored in the app from a webview in SWIFT

I have a webview and would like to show an image in the webview (html)

(I used ! as tagend and tagstart, because I don't know how to add this here without be interpreted as HTML, even I pasted as code)

I don't know how to do that in a best practice way. Any help ?

UPDATE I tried with referenced Article, but still not succeeded: My Code for this:

        let path:NSString = NSBundle.mainBundle().bundlePath;
        var baseURL:NSURL=NSURL(fileURLWithPath: path as String)!;
        var htmlString:String! = texttemp
        myWebView.loadHTMLString(htmlString, baseURL: baseURL)

The same Image I can already load like the following -> works:

var image = UIImage(named: "myimage.png");

Upvotes: 0

Views: 2163

Answers (1)

Duncan C
Duncan C

Reputation: 131426

Your updated code isn't right. You are creating a path to the bundle, not to the specific file. You need to use the NSBundle method pathForResource:ofType (or one of its variants) to build a path to your file. Then use that to create the URL.

The pathForResource:ofType family of methods return nil if the file can't be found, so you should check that you are getting back a path.

EDIT:

Looking at it more closely, I see that you are using the URL as the base URL for a call to loadHTMLString. This does look like a sound approach. What is your HTML string, and where is the image in your bundle?

Upvotes: 1

Related Questions