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

标签 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];

最佳答案

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

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

关于cocoa - 创建用于打印的 Webview 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5838520/

相关文章:

java - 将图像添加到 JFrame 时出现问题

jquery - 在表单中突出显示所选图像 - 使用 Jquery?

css - 图像以 Firefox 为中心,但在 Chrome 上被截断

c# - C#WPF WebBrowser替代品

macos - 检测 OS X 上的任何按键,包括修饰键

objective-c - NSString cStringUsingEncoding 返回值的生命周期

javascript - 如果图像来自另一个站点,将图像绘制到 Canvas 上是否会被描述为热链接?

css - webkit 滚动条上的不同宽度

swift - AppleScript(或 OSA)的功能是 Cocoa 的子集吗

objective-c - 返回某个对象的属性?