创建Webview以进行打印时出现问题 [英] Problems creating a Webview for printing

查看:483
本文介绍了创建Webview以进行打印时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Webview在使用Core Plot框架生成的图形周围添加一些文本以进行打印。我有以下代码,大部分似乎工作,但它不显示图像,但问号图像丢失的图像。任何人都可能指向正确的方向为什么问题可能是:

I'm trying to use a Webview to add some text around a graph produced using the Core Plot framework for printing. I have the following code which for the most part seems to work, however it is not displaying an image but the question mark image for a missing image. Can anyone point me in the right direction for what the problem might be:

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,例如:

You're using a regular path as the source of the image, which isn't valid HTML. You should use a file:// URL, like this:

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

这篇关于创建Webview以进行打印时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆