我在我的應用程序中有一個打印按鈕。如果我點擊按鈕,它應該打印屏幕。 我的問題是如果屏幕有一個滾動視圖,它的內容超過屏幕尺寸。它應該打印屏幕的全部內容。打印輸出可能需要2或3頁。是否有任何示例代碼或項目不是通過拍攝照片或屏幕截圖來打印屏幕。如何從我的iPhone應用程序打印?
3
A
回答
2
您需要將整個數據轉換爲HTML格式並使用WebView來顯示數據。爲了從iPhone應用程序打印出來,您需要從您的webview內容創建一個PDF文件,然後從這個pdf文件中打印出來。在這裏,我給你示例代碼,它在我的應用程序中正常工作。 1)包含QuartzCore框架並在ViewController.h文件中導入「QuartzCore/QuartzCore.h」。
2)在ViewController.h文件使用以下代碼
@interface ViewController : UIViewController<UIScrollViewDelegate, UIPrintInteractionControllerDelegate>
{
UIWebView *theWebView;
int imageName;
double webViewHeight;
}
-(void) takeSnapshot;
- (void) drawPdf;
-(void) printContent:(NSData *) data;
3)在viewDidLoad方法使用以下代碼:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *[email protected]"<html><body><img src=\"blue.png\" alt=\"Blue Image\" /> <h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p></body></html>";
UIBarButtonItem *generate=[[UIBarButtonItem alloc] initWithTitle:@"Print" style:UIBarButtonItemStylePlain target:self action:@selector(printBtnPressed)];
[self.navigationItem setRightBarButtonItem:generate];
theWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
[theWebView loadHTMLString:htmlString baseURL:nil];
theWebView.scrollView.bounces=NO;
[self.view addSubview:theWebView];
imageName=0;
webViewHeight=0.0;
}
4)方法定義
-(void) printBtnPressed
{
[self takeSnapshot];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir=[paths objectAtIndex:0];
NSString *filePath=[documentDir stringByAppendingPathComponent:@"Demo.pdf"];
NSData *data=[[NSData alloc] initWithContentsOfFile:filePath];
[self printContent:data];
}
-(void) takeSnapshot
{
webViewHeight=[[theWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] integerValue];
CGRect screenRect=theWebView.frame;
double currentWebViewHeight = webViewHeight;
while (currentWebViewHeight > 0)
{
imageName ++;
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[theWebView.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",imageName]];
if(currentWebViewHeight < 416)
{
CGRect lastImageRect = CGRectMake(0, 416 - currentWebViewHeight, theWebView.frame.size.width, currentWebViewHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect([newImage CGImage], lastImageRect);
newImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
}
[UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES];
[theWebView stringByEvaluatingJavaScriptFromString:@"window.scrollBy(0,416);"];
currentWebViewHeight -= 416;
}
[self drawPdf];
}
- (void) drawPdf
{
CGSize pageSize = CGSizeMake(612, webViewHeight);
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
double currentHeight = 0.0;
for (int index = 1; index <= imageName ; index++)
{
NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", index]];
UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath];
[pngImage drawInRect:CGRectMake(0, currentHeight, pageSize.width, pngImage.size.height)];
currentHeight += pngImage.size.height;
}
UIGraphicsEndPDFContext();
}
-(void) printContent:(NSData *) data
{
UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];
print.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
//printInfo.jobName = @"Information";
printInfo.duplex = UIPrintInfoDuplexLongEdge;
print.printInfo = printInfo;
print.showsPageRange = YES;
print.printingItem = data;
UIViewPrintFormatter *viewFormatter = [self.view viewPrintFormatter];
viewFormatter.startPage = 0;
print.printFormatter = viewFormatter;
UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {};
[print presentAnimated:YES completionHandler:completionHandler];
}
+1
+1,用於示例代碼 – NAZIK
0
這將是可能的take a screenshot programmatically,並打印。在這種情況下,您只需打印屏幕截圖。
1
可以將所有數據轉換成HTML,並打印出來,
- (void) didPressPrintExpenseButton {
UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];
printController.delegate = self;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
if (!completed && error) NSLog(@"Print error: %@", error);
};
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"Expense";
printController.printInfo = printInfo;
UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc]
initWithMarkupText:[self getHtmlInvoiceDescription]];
htmlFormatter.startPage = 0;
htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
htmlFormatter.maximumContentWidth = 6 * 72.0;
printController.printFormatter = htmlFormatter;
[htmlFormatter release];
printController.showsPageRange = YES;
[printController presentAnimated:YES completionHandler:completionHandler];
}
- (NSString *) getHtmlInvoiceDescription {
NSMutableString *htmlString = [[NSMutableString alloc]init];
[htmlString appendString:@"<html> \n"];
[htmlString appendString:@"<body> \n"];
[htmlString appendString:@"<table border='0' cellpadding='5' width='100%' >\n"];
[htmlString appendString:@"<tr>\n"];
[htmlString appendFormat:@"<td style=\"width='50%%'\"> %@ </td>", [self.addressString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"]];
//TODO change the image url here
UIImage * image = [UIImage imageNamed:@"iTunesArtwork.png"];
NSData *data = UIImagePNGRepresentation(image);
NSString * dataString = [data base64EncodedString]; // Your own base 64 converter
[htmlString appendFormat:@"<td style=\"width='50%%'\" align=\"right\"> <img src=\"data:image/png;base64,%@\" width='%.0f' height='%.0f' > </td>", dataString,
verifyHeader.frame.size.height * image.size.width/image.size.height, verifyHeader.frame.size.height];
[htmlString appendString:@"\n</tr> \n </table>"];
[htmlString appendFormat:@"\n<h2> %@ </h2>", NSLocalizedString(@"Expense", @"")];
[htmlString appendString:@"\n<table border='1' cellpadding='5' width='100%'>"];
[htmlString appendString:@"\n<tr>"];
[htmlString appendFormat:@"<td style=\"width='50%%' \"> %@ </td>", NSLocalizedString(@"Date: ", @"")];
[htmlString appendFormat:@"<td width='50%%'> %@ </td>", [self getFormattedDateString:self.expense.date]];
[htmlString appendString:@"</tr>"];
[htmlString appendString:@"\n<tr>"];
[htmlString appendFormat:@"<td> %@ </td>", NSLocalizedString(@"Reference no: ", @"")];
[htmlString appendFormat:@"<td> %@ </td>", self.expense.referenceNo];
[htmlString appendString:@"</tr>"];
[htmlString appendString:@"\n<tr>"];
[htmlString appendFormat:@"<td> %@ </td>", NSLocalizedString(@"Customer name: ", @"")];
[htmlString appendFormat:@"<td> %@ </td>", self.expense.customerName];
[htmlString appendString:@"</tr>"];
[htmlString appendString:@"\n<tr>"];
[htmlString appendFormat:@"<td> %@ </td>", NSLocalizedString(@"Product description: ", @"")];
[htmlString appendFormat:@"<td> %@ </td>", self.expense.productDescription];
[htmlString appendString:@"</tr>"];
[htmlString appendString:@"\n</table>"];
[htmlString appendString:@"\n</body> \n</html>"];
NSString * tempString = [NSString stringWithString:htmlString];
[htmlString release];
return tempString;
}
此外,還有轉換爲PDF您的信息的方式,然後你可以打印。 查找蘋果文檔iPhone print API guidelines。
相關問題
- 1. 如何從iPhone應用程序打印?
- 2. 從iphone應用程序打印
- 3. iphone:在我的iPhone應用程序添加打印功能
- 4. 從我的iPhone應用程序打開Spotify應用程序
- 5. 如何從我自己的應用程序打開iphone郵件應用程序?
- 6. 打印對象 - iPhone應用程序
- 7. 如何從Web應用程序打印收據打印機?
- 8. 如何從我的應用程序中打開iPhone日曆?
- 9. 如何從我自己的應用程序打開iPhone設置?
- 10. 我們如何從SWING應用程序打印文檔
- 11. 如何從Cocoa應用程序創建「打印實用程序」?
- 12. 如何從我的應用程序打開ola應用程序?
- 13. 如何打開我們的應用程序中的其他應用程序iphone
- 14. 是否可以從iPhone和iPad應用程序進行打印?
- 15. 如何從iPhone中的應用程序打開其他應用程序?
- 16. Android - 從移動打印機上的應用程序打印
- 17. 從藍牙打印機打印的Android應用程序
- 18. 如何從我的應用程序中打開設置頁後回調我的應用程序iphone
- 19. 如何從我的應用程序撥打電話,而不必在iPhone中退出我的應用程序?
- 20. 從.net應用程序批量打印?
- 21. 從silverlight應用程序打印文本
- 22. 自動打印從Web應用程序
- 23. 從可可應用程序打印NSImage
- 24. 從Windows Store應用程序打印
- 25. 從WinForms應用程序打印
- 26. 從網絡應用程序打印
- 27. 從Windows Mobile應用程序打印
- 28. 打印從iPad應用程序
- 29. 從C#應用程序打印命令
- 30. 如何打印從多線程應用程序
基本上你轉換滾動視圖到圖像文件然後打印。我做了同樣的事情,只發送圖像作爲電子郵件附件。 –