创建用于打印的Webview时出现问题

时间:2011-04-30 00:03:39

标签: cocoa image webkit

我正在尝试使用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];

1 个答案:

答案 0 :(得分:2)

您使用常规路径作为图片的来源,这是无效的HTML。您应该使用file:// URL,如下所示:

//...
NSURL *tempImageURL = [NSURL fileURLWithPath:temporaryImagePath];
[theURLString appendFormat:@"<img src=\"%@\"/>", [tempImageURL absoluteString]];
//...