将图像添加到HTML字符串iphone

时间:2013-01-21 11:53:14

标签: iphone ios objective-c

如何将我的Xcode项目中存在的图像添加到HTML字符串中以打印出该图像。

我尝试了以下代码,但图片确实打印了,而PDF中则出现一个空白框。

-(NSString *)prepareHTMLText {
    NSMutableString *body = [NSMutableString stringWithString:@"<!DOCTYPE html><html><body>"];


        [body appendString:@"<h2>Ingredients</h2>"];

     [body appendFormat:@"<p>%@ %@</p>", @"text", @"TEStText"];

   // [body appendFormat:@"<img src=\"%@\" alt=\"\" height=\"100\" width=\"100\"/>",[[NSBundle mainBundle] pathForResource:@"edit" ofType:@"png"]];

    [body appendFormat:@"<img src=\"%@\"/>",[UIImage imageNamed:@"edit.png"]];

    [body appendString:@"</body></html>"];
    return body;
}

我正如上所述得出结论 enter image description here

1 个答案:

答案 0 :(得分:2)

您需要一个URL,而不是文件路径或图像对象本身。尝试类似:

NSString *fileURLString = [[[NSBundle mainBundle] URLForResource:@"edit" withExtension:@"png"] absoluteString];
[body appendFormat:@"<img src=\"%@\"/>", fileURLString];