如何在UIMarkupTextPrintFormatter中添加图像?

我正在尝试使用直接的HTML打印文件,但是,我无法将图像添加到打印文件。

如何在我想要打印的HTML中引用项目中的图像? UIMarkupTextPrintFormatter支持标签吗?

实际上比我想象的要简单得多:

NSString *yourImagePath = [[[NSBundle mainBundle] URLForResource:@"resource" withExtension:@"extension"] absoluteString]; 

然后你可以把图像path放在<img> ,它会渲染图像! 瞧!

我一整天都在这里,无论如何将图像合并到我的打印作业中,似乎总是无法渲染它们。 我已经尝试了多种方法。 1)Base64embedded图像数据,2)文件pathURL例如。 “file:/// localhost / …”,3)基于string的文件path“/ Applications / ….”。 我一直在我的沙箱文件目录中存储的图像,并没有任何呈现出来。 在pdf中的其他一切都很好。 我也尝试了上述,甚至连捆绑的图像都没有呈现出来。 是什么赋予了? 我甚至把HTML放到一个文件中,并加载到我的电脑上的safari(因为在模拟器中testing的绝对path是相同的),它的工作原理。

资源

 + (NSData*)renderMarkup:(NSString*)markup { UIMarkupTextPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:markup]; SBAnnotationPrintPageRenderer *renderer = [[SBAnnotationPrintPageRenderer alloc] init]; [renderer addPrintFormatter:formatter startingAtPageAtIndex:0]; renderer.footerHeight = 10; renderer.headerHeight = 10; NSMutableData * pdfData = [NSMutableData data]; UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil); for (NSInteger i = 0; i < [renderer numberOfPages]; i++) { UIGraphicsBeginPDFPage(); CGRect bounds = UIGraphicsGetPDFContextBounds(); [renderer drawPageAtIndex:i inRect:bounds]; } UIGraphicsEndPDFContext(); return pdfData; } 

和生成的Html

 <table> <tr> <td> <img style='background-color:blue;' src='/Users/zachthayer/Library/Application Support/iPhone Simulator/6.1/Applications/A47C1BDF-DCD1-49A5-A452-59802AEC2A94/Documents/29161AFF-BE35-4EEE-A7A1-ACF2DA9ABA71.png'/> </td> <td> <h2>Superadmin</h2> <i>00:00:00:00</i> <p>Test note</p> </td> </tr> </table> 

和渲染的PDF

渲染PDF

在开发计算机上的safari中呈现相同的HTML

Safari渲染

本地图像应该首先放在一个imageView内,然后转换为base64值,并在html中的图像标签内使用它。

 let imageData:NSData = UIImagePNGRepresentation(imageView.image!)! let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength) var imageTag = String(format: "<img src=\"data:image/png;base64,%@\"", strBase64)