1

I have this NSString with html

NSString* html = [NSString stringWithFormat:@"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta name=\"viewport\" content=\"width=device-width\" /><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><title>Request</title><link rel=\"stylesheet\" type=\"text/css\" href=\"email.css\" /></head><body bgcolor=\"#FFFFFF\"><table class=\"body-wrap\"><tr><td></td><td class=\"container\" bgcolor=\"#FFFFFF\"><div class=\"content\"><table><tr><td><center><img src=\"./top.jpg\" style=\"margin-top: -5px; margin-bottom: 5px;\"/><img src=\"./bottom.jpg\" alt=\"\" style=\"width: 250px;\"/></center><h3>The User %@ %@</h3><p class=\"lead\">read this email.</p><p>REQUEST: %@</p><p>NAME: %@</p><p>SURNAME: %@</p><p>NUMBER: %@</p><p>CODE: %@</p><p>EMAIL: %@</p></td></tr></table></div></td><td></td></tr></table></body></html>", @"Name",@"Surname", @"Center", @"Name1", @"Cognome1", @"Number", @"code", @"email"];

In this html code there are two images: top.jpg and bottom.jpg, and I want to know waht's the way to pass at this code the local path of these two images, so I can visualize them.

thanks

2
  • Are the local paths to local to the iOS device or the local path of your computer? I'm a tad confused. Commented Jul 10, 2013 at 14:31
  • NSString *top_image = [[NSBundle mainBundle]pathForResource:@"top" ofType:@"jpg"]; NSString *bottom_image = [[NSBundle mainBundle]pathForResource:@"bottom" ofType:@"jpg"]; local in project iOS Commented Jul 10, 2013 at 14:32

3 Answers 3

2
NSString* html = [NSString stringWithFormat:@"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta name=\"viewport\" content=\"width=device-width\" /><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><title>Request</title><link rel=\"stylesheet\" type=\"text/css\" href=\"email.css\" /></head><body bgcolor=\"#FFFFFF\"><table class=\"body-wrap\"><tr><td></td><td class=\"container\" bgcolor=\"#FFFFFF\"><div class=\"content\"><table><tr><td><center><img src=\"./top.jpg\" style=\"margin-top: -5px; margin-bottom: 5px;\"/><img src=\"./bottom.jpg\" alt=\"\" style=\"width: 250px;\"/></center><h3>The User %@ %@</h3><p class=\"lead\">read this email.</p><p>REQUEST: %@</p><p>NAME: %@</p><p>SURNAME: %@</p><p>NUMBER: %@</p><p>CODE: %@</p><p>EMAIL: %@</p></td></tr></table></div></td><td></td></tr></table></body></html>", @"Name",@"Surname", @"Center", @"Name1", @"Cognome1", @"Number", @"code", @"email"];
UIWebView *temp = [[UIWebView alloc] initWithFrame:CGRectMake(startX, startY, width, height)];
NSData *htmlData = [self parseStringToHTML:html];
[temp loadData:_htmlData MIMEType:@"text/html" textEncodingName:@"utf-8"  baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

That should do the trick. The key thing is baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]

The baseURL tells the UIWebView where to look for any resources that it's contents might need.

Those who don't want to do anything with webViews but want to make images a part of your html string can do this : Add Base64 class www.imthi.com/wp-content/uploads/2010/08/base64.zip in your project. Now encode as:

NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);
[Base64 initialize];
NSString *strEncoded = [Base64 encode:data];

Now in your html string replace src=\"./bottom.jpg\" by an NSString with the following format:

@"src="data:image/jpg;base64,%@",strEncoded
Sign up to request clarification or add additional context in comments.

6 Comments

I should send this html in an email and not load it in a webview
I don't understand. Do you want to email this html to someone and not display it in a webView? If so, do you want to send these images with the html? Or will the receiver have them? Is the receiver an iOS device too?
yes, I don't show it in a webview, and I don't need "[temp loaddata..." but I need to sent this html with an email with "SKPSMTPMessage", it work all fine but I want show two images
actually you can convert those images to base64 online here base64-image.de
|
1

Swift 4

let data = UIImagePNGRepresentation(your_imageView) //be careful! If the image is a Jpeg you have to use UIImageJPEGRepresentation. Use the corresponding method to the image type.

if let imageITData = data?.base64EncodedString() {
    significatoHTMLContent = significatoHTMLContent.replacingOccurrences(of: "#FLAG-ITA#", with: "<img src=\"data:image/png;base64,\(imageITData)\" alt=\"Flag Ita\">")
}
//This is a my html file in which I insert an image. What you need to insert the image is that:<img src=\"data:image/png;base64,\(imageITData)\" alt=\"Flag Ita\">

Comments

0
if let image = yourObject.thePropertyUIImage, let data = image.pngData() {
        let base64 = data.base64EncodedString(options: [])
        let imageForTheHtml = "data:application/png;base64," + base64
        let html = "<html><head></head><body><img src='\(imageForTheHtml)'></body></html>"
        webView.loadHTMLString(html, baseURL: URL(fileURLWithPath: ""))
    }

Comments

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.