Ted
Ted

Reputation: 3885

How to refer a local image file from UIWebView's HTML on iPhone?

I would like to use <img src="temp.jpg" /> in my UIWebView's HTML.
How can I refer to a local file named temp.jpg to save precious loading time?

Of course the obvious choice would be <img src=".\temp.jpg" /> but I have no idea where my root is...

Upvotes: 2

Views: 7500

Answers (5)

ibrust
ibrust

Reputation: 187

Another option is to construct a base64image string from your local file in the swift code, and pass this string to your webview. you can render these base 64 strings by passing them to an img html tag directly. In my use case I needed to embed this image tag an HTML attributedString with a document type of html and render this html string within an actual UILabel ... I'll post some example code here, you may need to play around it, and you'd just pass the string to your webview instead -

let base64EncodedImage = UIImage.init(named: “my_image”).jpegData(compressionQuality: .leastNonzeroMagnitude)?.base64EncodedString()

let htmlString: String = "<div style='font-size:15px;display:inline;'> <b>hello internet</b> <img src='data:image/jpg;base64,\(base64image)' /> </div>"

let attributedText = try? NSMutableAttributedString(
    data: data,
    options: [.documentType: NSAttributedString.DocumentType.html,
              .characterEncoding: String.Encoding.utf8.rawValue],
    documentAttributes: nil
)

let label: UILabel = .init()

label.attributedText = attributedText

Upvotes: 0

Amr Angry
Amr Angry

Reputation: 3851

I was searching a lot for a solution and found the following which
in my case working fine, I have a local HTML string and a local image I convert the image into base 64

you may use this website to convert image to base64 encoding

Converter

and rewrite the HTML string to have the base64 image in the image tag like the following

    <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QCMRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAABmgAwAEAAAAAQAAABkAAAAA/+0AOFBob3Rvc2hvcCAzLjAAOEJJTQQEAAAAAAAAOEJJTQQlAAAAAAAQ1B2M2Y8AsgTpgAmY7PhCfv/AABEIABkAGQMBEQACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQI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7W9upbqS4ulaRlYpE0SoCsapwSpPRB3oASD4Pacv3nvG+t0y/+ggUAalh8NNMiIYWxYgggzTTS8g5BwzYzkCgDrsUAf//S9xoAKACgAoA//9k=" alt="Red dot" >

for Testing the result tye the following Online HTML editor

W3School

hope it will help for others or maybe get a better solution, best of luck

Upvotes: 2

WDUK
WDUK

Reputation: 19030

This will create a URL object that contains the location of your file on disk. You can then use this to output a valid file:// URL string that can be used within img tags.

NSURL* fileURL = [[NSBundle mainBundle] URLForResource:@"temp" withExtension:@"jpg"];

NSLog(@"<img src=\"%@\" />", fileURL.absoluteString);

Upvotes: 9

Jackson
Jackson

Reputation: 264

NSURL *url=[[NSBundle mainBundle] bundleURL];
[webView loadHTMLString:string baseURL:url];

This let's the root be the app's main bundle.

Upvotes: 4

user529758
user529758

Reputation:

I have no idea where my root is...

UIWebView looks up files relative to the HTML file you loaded in it. So perhaps it will be your app bundle.

Upvotes: 0

Related Questions