1

我有这个带有 html 的 NSString

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"];

在这个 html 代码中有两个图像:top.jpg 和 bottom.jpg,我想知道在这段代码中传递这两个图像的本地路径的方法是什么,所以我可以将它们可视化。

谢谢

4

3 回答 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]]];

这应该够了吧。关键是baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]

baseURL 告诉 UIWebView 在哪里寻找它的内容可能需要的任何资源。

那些不想对 webViews 做任何事情但想让图像成为您的 html 字符串的一部分的人可以这样做:您的项目。现在编码为:

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

现在在您的 html 字符串中,将 src=\"./bottom.jpg\" 替换为具有以下格式的 NSString:

@"src="data:image/jpg;base64,%@",strEncoded
于 2013-07-10T14:42:26.993 回答
1

斯威夫特 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\">
于 2018-08-15T18:26:17.883 回答
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: ""))
    }
于 2019-05-09T13:36:09.457 回答