图形未出现在 Web 视图中

发布于 2024-11-04 09:21:09 字数 2635 浏览 0 评论 0原文

这个问题是上一个问题的后续问题: 创建用于打印的 Webview 时出现问题

我正在使用 Webview 来打印使用创建的绘图核心情节框架。以下代码用于填充 Web 视图:

    NSData        *imageData = [[barChart imageOfLayer] TIFFRepresentation];
NSString      *temporaryImagePath = NSTemporaryDirectory();
NSError       *error = nil;

NSBitmapImageRep*   imageRep    = [NSBitmapImageRep imageRepWithData:imageData];
NSData*             pngData;

pngData = [imageRep representationUsingType:NSJPEGFileType 
                                 properties:nil];

temporaryImagePath = [temporaryImagePath stringByAppendingPathComponent:@"jpegData.jpg" ];
[pngData writeToFile:temporaryImagePath 
               options:NSDataWritingAtomic 
                 error:&error ];

WebView *printView;

printView =[[WebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 1000)
                                frameName:@"printFrame" 
                                groupName:@"printGroup"];

NSString *theCSSString = [self theFileCodeForString:@"OutputFormat"
                                      withExtension:@"CSS"
                                       withContents:@"<style type=\"text/css\">#results {font-family:\"Trebuchet MS\", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;} #results td, #results th {font-size:1em;border:1px solid #98bf21;padding:3px 7px 2px 7px;} #results th {font-size:1.1em;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#A7C942;color:#ffffff;} #results tr.alt td {color:#000000;background-color:#EAF2D3;} </style>"];

NSMutableString *theURLString = [[NSMutableString alloc] initWithString:@"<html>"];
[theURLString appendString:@"<head>"];
[theURLString appendString:theCSSString];
[theURLString appendString:@"</head>"];

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

[[printView mainFrame] loadHTMLString:theURLString
                  baseURL:[NSURL URLWithString:@"xxxx"]];

[[[[printView mainFrame] frameView] documentView] print:sender];
[printView release];

我的问题是图像没有出现在 Web 视图的打印版本中,或者如果我实例化一个包含 Web 视图的窗口,则图像不会出现。如果我获取代码生成的原始 HTML,将其粘贴到 Coda 中,然后预览它,则图像不会出现任何问题。谁能指出我如何解决这个问题的正确方向。这可能是一个相当基本的错误,因为我对使用图像和 Web 视图相当陌生。

编辑

看起来问题与图像链接的格式有关,因为当我用 html://... 链接替换生成的 file:///... 链接时网络上的图像似乎一切正常。这并不能解释为什么它在 Coda 中有效但在 Webview 中无效。

This question follows on from a previous question:
Problems creating a Webview for printing

I am using a Webview to print plots created using the Core Plot framework. The following code is being used to populate the Webview:

    NSData        *imageData = [[barChart imageOfLayer] TIFFRepresentation];
NSString      *temporaryImagePath = NSTemporaryDirectory();
NSError       *error = nil;

NSBitmapImageRep*   imageRep    = [NSBitmapImageRep imageRepWithData:imageData];
NSData*             pngData;

pngData = [imageRep representationUsingType:NSJPEGFileType 
                                 properties:nil];

temporaryImagePath = [temporaryImagePath stringByAppendingPathComponent:@"jpegData.jpg" ];
[pngData writeToFile:temporaryImagePath 
               options:NSDataWritingAtomic 
                 error:&error ];

WebView *printView;

printView =[[WebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 1000)
                                frameName:@"printFrame" 
                                groupName:@"printGroup"];

NSString *theCSSString = [self theFileCodeForString:@"OutputFormat"
                                      withExtension:@"CSS"
                                       withContents:@"<style type=\"text/css\">#results {font-family:\"Trebuchet MS\", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;} #results td, #results th {font-size:1em;border:1px solid #98bf21;padding:3px 7px 2px 7px;} #results th {font-size:1.1em;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#A7C942;color:#ffffff;} #results tr.alt td {color:#000000;background-color:#EAF2D3;} </style>"];

NSMutableString *theURLString = [[NSMutableString alloc] initWithString:@"<html>"];
[theURLString appendString:@"<head>"];
[theURLString appendString:theCSSString];
[theURLString appendString:@"</head>"];

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

[[printView mainFrame] loadHTMLString:theURLString
                  baseURL:[NSURL URLWithString:@"xxxx"]];

[[[[printView mainFrame] frameView] documentView] print:sender];
[printView release];

My problem is that the image is not appearing in either the printed version of the Webview or if I instantiate a window with the Webview in it the image does not appear. If I take the raw HTML generated by the code though, paste it into Coda and then preview it the image appears with no problems. Can anyone point me in the right direction for how to fix this. It is probably a fairly basic error as I'm fairly new to working with images and Webviews.

Edit

It looks like the problem relates to the format of the link to the image because when I replace the generated file:///... link with a html://... link with an image on the web everything seems to work fine. This doesn't explain why it works in Coda but not in the Webview.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2024-11-11 09:21:09

问题解决了。将 baseURL 设置为 tempImageURL 解决了问题。一切都很好。

Problem solved. Setting the baseURL to tempImageURL solved the problem. Everything is good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文