我正在尝试使用 Webview 在使用 Core Plot 框架生成的图表周围添加一些文本进行打印。我有以下代码,在很大程度上似乎可以工作,但是它不显示图像,而是显示丢失图像的问号图像。谁能指出我可能遇到的问题的正确方向:
NSData *imageData = [[barChart imageOfLayer] TIFFRepresentation];
NSString *temporaryImagePath = NSTemporaryDirectory();
NSError *error = nil;
NSBitmapImageRep* imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSData* pngData;
pngData = [imageRep representationUsingType:NSPNGFileType
properties:nil];
temporaryImagePath = [temporaryImagePath stringByAppendingPathComponent:@"pngData.png" ];
[pngData writeToFile:temporaryImagePath
options:NSDataWritingAtomic
error:&error ];
WebView *printView;
printView =[[WebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 500)
frameName:@"printFrame"
groupName:@"printGroup"];
NSMutableString *theURLString = [[NSMutableString alloc] initWithString:@"<html>"];
[theURLString appendString:@"<head>"];
[theURLString appendString:@"</head>"];
[theURLString appendString:@"<body>"];
[theURLString appendString:[NSString stringWithFormat:@"<img src=\"%@\" alt=\"Histogram\"/>", temporaryImagePath]];
[theURLString appendString:@"</body></html>"];
[[printView mainFrame] loadHTMLString:theURLString
baseURL:[NSURL URLWithString:@"a URL"]];
[[[[printView mainFrame] frameView] documentView] print:sender];
[printView release];
最佳答案
您使用常规路径作为图像源,这不是有效的 HTML。您应该使用 file://URL,如下所示:
//...
NSURL *tempImageURL = [NSURL fileURLWithPath:temporaryImagePath];
[theURLString appendFormat:@"<img src=\"%@\"/>", [tempImageURL absoluteString]];
//...
关于cocoa - 创建用于打印的 Webview 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5838520/