내 앱에 인쇄 버튼이 있습니다. 버튼을 클릭하면 화면이 인쇄됩니다. 내 문제는 화면에 스크롤보기가 있고 내용이 화면 크기보다 큰 경우입니다. 화면의 전체 내용을 인쇄해야합니다. 인쇄물에 2 ~ 3 페이지가 걸릴 수 있습니다. 사진이나 스크린 샷을 찍지 않고 화면을 인쇄 할 수있는 샘플 코드 나 프로젝트가 있습니까?iPhone 앱에서 출력물을 가져 오는 방법은 무엇입니까?
3
A
답변
2
전체 데이터를 HTML 형식으로 변환하고 WebView를 사용하여 데이터를 표시해야합니다. iPhone app에서 출력물을 가져 오려면 webview 콘텐츠에서 PDF 파일을 만든 다음이 pdf 파일에서 인쇄물을 가져와야합니다. 여기 예제 코드를 제공하고 있습니다. 제대로 작동하고 있습니다.
1) QuartzCore 프레임 워크를 포함하고 ViewController.h 파일에 "QuartzCore/QuartzCore.h"를 임포트하십시오. 다음 코드를 ViewController.h 파일 사용에
2) 코드 다음의 viewDidLoad 방법 사용에
@interface ViewController : UIViewController<UIScrollViewDelegate, UIPrintInteractionControllerDelegate>
{
UIWebView *theWebView;
int imageName;
double webViewHeight;
}
-(void) takeSnapshot;
- (void) drawPdf;
-(void) printContent:(NSData *) data;
3) :
- (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
당신은
- (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에서 정보를 변환하는 방법이 한 다음 그것을 인쇄 할 수 있습니다, HTML로 모든 데이터를 변환하고 인쇄 할 수 있습니다. 사과 설명서
iPhone print API guidelines을 찾으십시오.
관련 문제
- 1. 내 iPhone 앱에서 짧은 URL을 가져 오는 방법은 무엇입니까?
- 2. PLActorKit을 가져 오는 방법은 무엇입니까?
- 3. 내 앱에서 사용자 이름과 비밀번호를 가져 오는 방법은 무엇입니까?
- 4. MXML 앱에서 데이터베이스의 값을 가져 오는 방법은 무엇입니까?
- 5. ASP.MVC 앱에서 뷰를 정의하고 어셈블리에서 가져 오는 방법은 무엇입니까?
- 6. 내 iPhone 앱에서 Gmail 주소록을 가져 오는 방법 앱
- 7. iPhone에서 XLS 데이터를 가져 오는 방법은 무엇입니까?
- 8. UITextField에서 문자열을 가져 오는 방법은 무엇입니까?
- 9. 이메일을 가져 오는 방법은 무엇입니까?
- 10. 활동간에 매개 변수를 가져 오는 방법은 무엇입니까?
- 11. iPhone 프로젝트에서 FBConnect 폴더를 가져 오는 방법은 무엇입니까?
- 12. iPhone 프로그램의 인증서에서 공개 키를 가져 오는 방법은 무엇입니까?
- 13. iPhone 앱에서 걸려 오는 전화를 "거부"합니까?
- 14. iPhone의 WebService에서 데이터를 가져 오는 방법은 무엇입니까?
- 15. iPhone 앱에서 사진을 공유하는 방법은 무엇입니까?
- 16. iPhone 앱에서 실행시 선택할보기를 선택하는 방법은 무엇입니까?
- 17. iPhone 앱에서 Drupal 사이트에 로그인하는 방법은 무엇입니까?
- 18. iphone 앱에서 페이지 컨트롤을 구현하는 방법은 무엇입니까?
- 19. iPhone 앱에서 트위터 인증을 요청하는 방법은 무엇입니까?
- 20. 앱에서 iPhone Safari History에 액세스하는 방법은 무엇입니까?
- 21. iPhone 앱에서 GPS를 구현하는 방법은 무엇입니까?
- 22. 앱에서 iPhone OS 버전을 감지하는 방법은 무엇입니까?
- 23. 내 앱에서 iPhone 캘린더를 여는 방법은 무엇입니까?
- 24. iphone 앱에서 배경 화면을 표시하는 방법은 무엇입니까?
- 25. iPhone 앱에서 네트워크 상태를 확인하는 방법은 무엇입니까?
- 26. iPhone 앱에서 시작 화면을 설정하는 방법은 무엇입니까?
- 27. iPhone 앱에서 쿠키를 설정/확인하는 방법은 무엇입니까?
- 28. iphone 앱에서 실시간 데이터를 표시하는 방법은 무엇입니까?
- 29. iphone 앱에서 inapppurchase를 사용하는 방법은 무엇입니까?
- 30. iPhone 배터리가 충전 중인지 여부를 iPhone 앱에서 감지하는 방법은 무엇입니까?
는 기본적 변환 스크롤하여 이미지 파일을보고 인쇄하십시오. 이메일 첨부 파일로만 이미지를 보내는 것과 같은 일을했습니다. –