2011-04-30 45 views
0

我想使用Webview來添加一些圍繞使用Core Plot框架打印的圖形的文本。我有以下代碼似乎大部分似乎工作,但它不是顯示圖像,而是缺少圖像的問號圖像。任何人都可以點我什麼問題可能是朝着正確的方向:創建Webview打印的問題

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

回答

2

您使用的是常規路徑的形象,這不是有效的HTML源。您應該使用file:// URL,如下所示:

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

感謝您的幫助。當你看它時真的很明顯的錯誤。 – 2011-04-30 10:08:42